Index: core/bootstrap/src/org/netbeans/CLIHandler.java =================================================================== RCS file: /cvs/core/bootstrap/src/org/netbeans/CLIHandler.java,v retrieving revision 1.10 diff -u -r1.10 CLIHandler.java --- core/bootstrap/src/org/netbeans/CLIHandler.java 12 Dec 2003 16:39:04 -0000 1.10 +++ core/bootstrap/src/org/netbeans/CLIHandler.java 7 Jan 2004 12:49:04 -0000 @@ -86,9 +86,7 @@ protected abstract int cli(Args args); private static void showHelp(PrintWriter w, List handlers) { - w.println("-?"); - w.println("--help"); - w.println(" Show this help information."); +// w.println(" -? or --help Show this help information."); Iterator it = handlers.iterator(); while (it.hasNext()) { ((CLIHandler)it.next()).usage(w); @@ -128,16 +126,14 @@ private static int notifyHandlers(Args args, List handlers, int when, boolean failOnUnknownOptions, boolean consume) { try { //System.err.println("notifyHandlers: handlers=" + handlers + " when=" + when + " args=" + Arrays.asList(args.getArguments())); - if (failOnUnknownOptions) { - String[] argv = args.getArguments(); - for (int i = 0; i < argv.length; i++) { - assert argv[i] != null; - if (argv[i].equals("-?") || argv[i].equals("--help") || argv[i].equals ("-help")) { // NOI18N - PrintWriter w = new PrintWriter(args.getOutputStream()); - showHelp(w, handlers); - w.flush(); - return 2; - } + String[] argv = args.getArguments(); + for (int i = 0; i < argv.length; i++) { + assert argv[i] != null; + if (argv[i].equals("-?") || argv[i].equals("--help") || argv[i].equals ("-help")) { // NOI18N + PrintWriter w = new PrintWriter(args.getOutputStream()); + showHelp(w, handlers); + w.flush(); + return 2; } } int r = 0; @@ -153,7 +149,7 @@ } } if (failOnUnknownOptions) { - String[] argv = args.getArguments(); + argv = args.getArguments(); for (int i = 0; i < argv.length; i++) { if (argv[i] != null) { // Unhandled option. @@ -240,8 +236,8 @@ * @param cleanLockFile removes lock file if it appears to be dead * @return the file to be used as lock file or null parsing of args failed */ - static Status initialize(String[] args, ClassLoader loader, boolean failOnUnknownOptions, boolean cleanLockFile) { - return initialize(new Args(args, System.in, System.err, System.getProperty ("user.dir")), (Integer)null, allCLIs(loader), failOnUnknownOptions, cleanLockFile); + static Status initialize(String[] args, InputStream is, OutputStream os, ClassLoader loader, boolean failOnUnknownOptions, boolean cleanLockFile) { + return initialize(new Args(args, is, os, System.getProperty ("user.dir")), (Integer)null, allCLIs(loader), failOnUnknownOptions, cleanLockFile); } /** Index: core/bootstrap/src/org/netbeans/Main.java =================================================================== RCS file: /cvs/core/bootstrap/src/org/netbeans/Main.java,v retrieving revision 1.15 diff -u -r1.15 Main.java --- core/bootstrap/src/org/netbeans/Main.java 12 Dec 2003 16:39:04 -0000 1.15 +++ core/bootstrap/src/org/netbeans/Main.java 7 Jan 2004 12:49:04 -0000 @@ -29,6 +29,48 @@ * @throws Exception for lots of reasons */ public static void main (String args[]) throws Exception { + java.lang.reflect.Method[] m = new java.lang.reflect.Method[1]; + int res = execute (args, System.in, System.err, m); + if (res == -1) { + // Connected to another running NB instance and succeeded in making a call. + System.exit(0); + } else if (res != 0) { + // Some CLIHandler refused the invocation + System.exit(res); + } + + m[0].invoke (null, new Object[] { args }); + } + + /** Returns string describing usage of the system. Does that by talking to + * all registered handlers and asking them to show their usage. + * + * @return the usage string for the system + */ + public static String usage () throws Exception { + java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream (); + + String[] newArgs = { "--help" }; + + int res = execute (newArgs, System.in, os, null); + return new String (os.toByteArray ()); + } + + /** Constructs the correct ClassLoader, finds main method to execute + * and invokes all registered CLIHandlers. + * + * @param args the arguments to pass to the handlers + * @param reader the input stream reader for the handlers + * @param writer the output stream for the handlers + * @param methodToCall null or array with one item that will be set to + * a method that shall be executed as the main application + */ + private static int execute ( + String[] args, + java.io.InputStream reader, + java.io.OutputStream writer, + java.lang.reflect.Method[] methodToCall + ) throws Exception { ArrayList list = new ArrayList (); String home = System.getProperty ("netbeans.home"); // NOI18N @@ -98,7 +140,7 @@ // CLIHandler.Status result; - result = CLIHandler.initialize(args, loader, true, false); + result = CLIHandler.initialize(args, reader, writer, loader, true, false); if (result.getExitCode () == CLIHandler.Status.CANNOT_CONNECT) { int value = javax.swing.JOptionPane.showConfirmDialog ( null, @@ -108,20 +150,16 @@ javax.swing.JOptionPane.WARNING_MESSAGE ); if (value == javax.swing.JOptionPane.OK_OPTION) { - result = CLIHandler.initialize(args, loader, true, true); + result = CLIHandler.initialize(args, reader, writer, loader, true, true); } } - int res = result.getExitCode(); - if (res == -1) { - // Connected to another running NB instance and succeeded in making a call. - System.exit(0); - } else if (res != 0) { - // Some CLIHandler refused the invocation - System.exit(res); + + if (methodToCall != null) { + methodToCall[0] = m; } - m.invoke (null, new Object[] { args }); + return result.getExitCode (); } /** Index: core/release/bin/runide.sh =================================================================== RCS file: /cvs/core/release/bin/runide.sh,v retrieving revision 1.50 diff -u -r1.50 runide.sh --- core/release/bin/runide.sh 11 Sep 2003 17:46:50 -0000 1.50 +++ core/release/bin/runide.sh 7 Jan 2004 12:49:05 -0000 @@ -95,39 +95,23 @@ while [ $# -gt 0 ] ; do # echo "Processing arg: '$1'" case "$1" in - -h|-help) cat >&2 <&2 < - path to JDK (could also be just JRE but some modules may not work!) - -userdir - specifies user settings directory (${userdir} by default) - -J - passes to JVM -Classpath options (normally you should NOT use these except to support -ui): - -cp:p - prepends to IDE's classpath - -cp:a - appends to IDE's classpath - -All other options and arguments are passed to the IDE, so try -? for more. -Any options found in $HOME/ide.cfg or else $idehome/bin/ide.cfg -are treated as defaults (may be overridden). -See documentation for details. +General options: + --help show this help + --jdkhome path to JDK (could also be just JRE but some modules may not work!) + -J passes to JVM +Classpath options: + --cp:p prepends to classpath + --cp:a appends to classpath EOF -exit 2 -;; - -jdkhome) shift; if [ $# -gt 0 ] ; then jdkhome=$1; fi;; - -userdir) shift; if [ $# -gt 0 ] ; then userdir=$1; fi;; + # go on and print IDE options as well + args="$args --help" ;; + -jdkhome|--jdkhome) shift; if [ $# -gt 0 ] ; then jdkhome=$1; fi;; # For compatibility only: -mainclass) shift; if [ $# -gt 0 ] ; then ide_class_option=-Dnetbeans.mainclass=$1; fi;; - -cp|-cp:a) + -cp|-cp:a|--cp|--cp:a) shift; if [ $# -gt 0 ] ; then if [ ! -z "$postfixcp" ] ; then postfixcp="$postfixcp:" ; fi @@ -135,7 +119,7 @@ fi ;; - -cp:p) + -cp:p|--cp:p) shift; if [ $# -gt 0 ] ; then if [ ! -z "$prefixcp" ] ; then prefixcp="$prefixcp:" ; fi @@ -174,14 +158,14 @@ if [ -z "$jdkhome" ] ; then echo "Cannot find JDK. Please set the JDK_HOME environment variable to point" >&2 - echo "to your JDK installation directory, or use the -jdkhome switch" >&2 + echo "to your JDK installation directory, or use the --jdkhome switch" >&2 exit 2 fi if [ ! -x "${jdkhome}/bin/java" ] ; then echo "Cannot find JDK at ${jdkhome}. Please set the JDK_HOME" >&2 echo "environment variable to point to your JDK installation directory," >&2 - echo "or use the -jdkhome switch" >&2 + echo "or use the --jdkhome switch" >&2 exit 2 fi @@ -190,7 +174,7 @@ # if [ ! -z "$userdir" ] ; then - jargs="-Dnetbeans.user=\"${userdir}\" $jargs" + args="--userdir \"${userdir}\" $args" fi # Index: core/src/org/netbeans/core/Bundle.properties =================================================================== RCS file: /cvs/core/src/org/netbeans/core/Bundle.properties,v retrieving revision 1.375 diff -u -r1.375 Bundle.properties --- core/src/org/netbeans/core/Bundle.properties 20 Nov 2003 10:37:33 -0000 1.375 +++ core/src/org/netbeans/core/Bundle.properties 7 Jan 2004 12:49:16 -0000 @@ -150,21 +150,21 @@ # NonGui TEXT_help=\ Command-line options:\n\ - \ -? show this help\n\ - \ -h (or -help) show launcher-specific options\n\ - \ -ui use given UI class as the IDE's Look & Feel\n\ - \ -fontsize use given number as the base font size of the\n\ + \ --ui use given UI class as the IDE's Look & Feel\n\ + \ --fontsize use given number as the base font size of the\n\ \ IDE user interface, in points (11 default)\n\ \ (but see http://ui.netbeans.org/docs/ui/themes/themes.html)\n\ - \ -locale use specified locale\n\ - Rarer options (definitely not supported!):\n\ - \ -branding use specified branding (- for default)\n\ - \ -nologging do not create the log file\n\ - \ -nosplash do not show the splash screen\n\ - \ -nogui just start up internals, do not show GUI + \ --locale use specified locale\n\ + \ --userdir user settings directory (${userdir} by default)\n\ + \ --branding use specified branding (- for default) +# +# \ --nologging do not create the log file\n\ +# \ --nosplash do not show the splash screen\n\ +# \ --nogui just start up internals, do not show GUI # error messages ERR_UIExpected=UI class name expected, using default UI... +ERR_UserDirExpected=Directory expected after --userdir switch ERR_UINotFound=UI class not found, using default UI... ERR_UIError=An error occured when setting the specified UI, using default UI ... ERR_FontSizeExpected=Font size expected, using default font size... Index: core/src/org/netbeans/core/CLIOptions.java =================================================================== RCS file: /cvs/core/src/org/netbeans/core/CLIOptions.java,v retrieving revision 1.3 diff -u -r1.3 CLIOptions.java --- core/src/org/netbeans/core/CLIOptions.java 7 Oct 2003 20:30:42 -0000 1.3 +++ core/src/org/netbeans/core/CLIOptions.java 7 Jan 2004 12:49:16 -0000 @@ -46,6 +46,17 @@ return cli(arguments.getArguments()); } + private static boolean isOption (String value, String optionName) { + if (value == null) return false; + + if (value.startsWith ("--")) { + return value.substring (2).equals (optionName); + } else if (value.startsWith ("-")) { + return value.substring (1).equals (optionName); + } + return false; + } + final int cli(String[] args) { // let's go through the command line for (int i = 0; i < args.length; i++) { @@ -53,15 +64,23 @@ continue; } boolean used = true; - if (args[i].equalsIgnoreCase("-nogui")) { // NOI18N + if (isOption (args[i], "nogui")) { // NOI18N System.getProperties().put("org.openide.TopManager", "org.netbeans.core.NonGui"); // NOI18N - } else if (args[i].equalsIgnoreCase("-nosplash")) { // NOI18N + } else if (isOption (args[i], "nosplash")) { // NOI18N NonGui.noSplash = true; - } else if (args[i].equalsIgnoreCase("-noinfo")) { // NOI18N + } else if (isOption (args[i], "noinfo")) { // NOI18N // obsolete switch, ignore - } else if (args[i].equalsIgnoreCase("-nologging")) { // NOI18N + } else if (isOption (args[i], "nologging")) { // NOI18N NonGui.noLogging = true; - } else if (args[i].equalsIgnoreCase("-ui")) { // NOI18N + } else if (isOption (args[i], "userdir")) { // NOI18N + args[i] = null; + try { + System.setProperty ("netbeans.user", args[++i]); + } catch(ArrayIndexOutOfBoundsException e) { + System.err.println(NonGui.getString("ERR_UserDirExpected")); + return 2; + } + } else if (isOption (args[i], "ui")) { // NOI18N args[i] = null; try { NonGui.uiClass = Class.forName(args[++i]); @@ -72,7 +91,7 @@ System.err.println(NonGui.getString("ERR_UINotFound")); return 1; } - } else if (args[i].equalsIgnoreCase("-fontsize")) { // NOI18N + } else if (isOption (args[i], "fontsize")) { // NOI18N args[i] = null; try { NonGui.uiFontSize = Integer.parseInt(args[++i]); @@ -83,7 +102,7 @@ System.err.println(NonGui.getString("ERR_BadFontSize")); return 1; } - } else if (args[i].equalsIgnoreCase("-locale")) { // NOI18N + } else if (isOption (args[i], "locale")) { // NOI18N args[i] = null; String localeParam = args[++i]; String language; @@ -103,7 +122,7 @@ country = localeParam.substring(index1+1); } Locale.setDefault(new Locale(language, country, variant)); - } else if (args[i].equalsIgnoreCase("-branding")) { // NOI18N + } else if (isOption (args[i], "branding")) { // NOI18N args[i] = null; String branding = args[++i]; if (branding.equals("-")) branding = null; // NOI18N Index: openide/src/org/openide/text/CloneableEditorSupport.java =================================================================== RCS file: /cvs/openide/src/org/openide/text/CloneableEditorSupport.java,v retrieving revision 1.104 diff -u -r1.104 CloneableEditorSupport.java --- openide/src/org/openide/text/CloneableEditorSupport.java 12 Dec 2003 09:00:03 -0000 1.104 +++ openide/src/org/openide/text/CloneableEditorSupport.java 7 Jan 2004 12:50:43 -0000 @@ -106,9 +106,7 @@ private EditorKit kit; /** document we work with */ - private StyledDocument doc; - - + private DocReference docRef; /** Non default MIME type used to editing */ private String mimeType; @@ -251,6 +249,24 @@ final EditorKit kit () { return kit; } + + /** Getter for the document we are associated with. + */ + final StyledDocument doc () { + return doc (true); + } + /** Getter for the document we are associated with. + * @param wait whether to wait for the result or not + */ + final StyledDocument doc (boolean wait) { + Object o = docRef; + if (o instanceof StyledDocument) { + return (StyledDocument)o; + } else { + DocReference ref = (DocReference)o; + return ref == null ? null : (StyledDocument)ref.get (wait); + } + } /** @@ -377,16 +393,21 @@ // in spite of that the document is not yet fully read in kit = createEditorKit (); - if (doc == null) { - doc = createStyledDocument (kit); + StyledDocument styled = doc (); + if (styled == null) { + styled = createStyledDocument (kit); + docRef = new DocReference (styled); } - final StyledDocument docToLoad = doc; // The thread nume should be: "Loading document " + env; // NOI18N - prepareTask = RequestProcessor.getDefault().post(new Runnable () { - + class R implements Runnable { + private StyledDocument docToLoad; private boolean runningInAtomicLock; + public R (StyledDocument style) { + this.docToLoad = style; + } + public void run () { // Run the operations under atomic lock primarily due @@ -398,8 +419,8 @@ // Add undoable listener after atomic change has finished synchronized (getLock()) { - if (doc == docToLoad) { // if document still valid - doc.addUndoableEditListener(getUndoRedo()); + if (doc() == docToLoad && docToLoad != null) { // if document still valid + docToLoad.addUndoableEditListener(getUndoRedo()); } } @@ -413,7 +434,7 @@ } // Check whether the document to be loaded was not closed - if (doc != docToLoad) { + if (doc() != docToLoad) { return; // do not load closed document } @@ -432,7 +453,7 @@ updateLineSet(true); - fireDocumentChange(doc, true); + fireDocumentChange(docToLoad, true); clearDocument(); } @@ -445,7 +466,7 @@ // assign before fireDocumentChange() as listener should be able to access getDocument() documentStatus = DOCUMENT_READY; - fireDocumentChange(doc, false); + fireDocumentChange(docToLoad, false); // Confirm that whole loading succeeded targetStatus = DOCUMENT_READY; @@ -456,7 +477,7 @@ throw t; } finally { - + docToLoad = null; synchronized (getLock()) { documentStatus = targetStatus; @@ -466,19 +487,23 @@ } } - }); + } + prepareTask = RequestProcessor.getDefault().post(new R (styled)); return prepareTask; } /** Clears the doc document. Helper method. */ private void clearDocument() { - NbDocument.runAtomic(doc, new Runnable() { + final StyledDocument style = doc (); + if (style == null) return; + + NbDocument.runAtomic(style, new Runnable() { public void run() { try { - doc.removeDocumentListener(getListener()); - doc.remove(0, doc.getLength()); // remove all text - doc.addDocumentListener(getListener()); + style.removeDocumentListener(getListener()); + style.remove(0, style.getLength()); // remove all text + style.addDocumentListener(getListener()); } catch(BadLocationException ble) { ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, ble); @@ -528,7 +553,7 @@ case DOCUMENT_RELOADING: // proceed to DOCUMENT_READY case DOCUMENT_READY: - return doc; + return doc(); default: // loading try { @@ -563,7 +588,7 @@ // (possible only via LineListener->DocumentLine..). // PENDING Needs to be tried to redesign DocumentLine to avoid this. if (LOCAL_LOAD_TASK.get() != null) { - return doc; + return doc(); } try { @@ -634,7 +659,7 @@ // remember time of last save lastSaveTime = System.currentTimeMillis(); - notifyUnmodified (); + doNotifyUnmodified (); } catch (BadLocationException ex) { ErrorManager.getDefault().notify(ex); @@ -861,6 +886,7 @@ * @return true if document is loaded */ public boolean isDocumentLoaded() { + doc (); return loadTask != null; } @@ -1022,6 +1048,7 @@ * by calling prepareDocument(). */ protected Task reloadDocument() { + StyledDocument doc = doc (); if (doc != null) { // UndoManager must be detached from document here because it will be attached in loadDocument() doc.removeUndoableEditListener (getUndoRedo ()); @@ -1069,7 +1096,7 @@ ) ); - notifyUnmodified (); + doNotifyUnmodified (); updateLineSet(true); } }); @@ -1134,6 +1161,21 @@ return sd; } + // + // Ensuring some behaviour is done when modified/unmodified + // + + private void doNotifyModified () { + notifyModified (); + docRef.hold (true); + } + + private void doNotifyUnmodified () { + notifyUnmodified (); + docRef.hold (false); + } + + /** Notification method called when the document become unmodified. * Called after save or after reload of document. *

@@ -1230,7 +1272,7 @@ private Runnable createUndoTask() { return new Runnable() { public void run() { - StyledDocument sd = doc; + StyledDocument sd = doc (); if(sd == null) { // #20883, doc can be null(!), doCloseDocument was faster. return; @@ -1269,7 +1311,7 @@ /** Allows access to the document without any checking. */ final StyledDocument getDocumentHack () { - return doc; + return doc (); } @@ -1295,6 +1337,7 @@ Line.Set oldSet = lineSet; + StyledDocument doc = doc (); if (doc == null || documentStatus == DOCUMENT_RELOADING) { lineSet = new EditorSupportLineSet.Closed(CloneableEditorSupport.this); } else { @@ -1424,15 +1467,16 @@ // notifies the support that env ().removePropertyChangeListener(getListener()); - notifyUnmodified (); + doNotifyUnmodified (); + StyledDocument doc = doc (false); if (doc != null) { getUndoRedo().discardAllEdits(); doc.removeUndoableEditListener (getUndoRedo ()); doc.removeDocumentListener(getListener()); } - if (positionManager != null) { + if (positionManager != null && doc != null) { positionManager.documentClosed (); documentStatus = DOCUMENT_NO; @@ -1440,7 +1484,7 @@ } documentStatus = DOCUMENT_NO; - doc = null; + docRef = null; kit = null; @@ -1459,7 +1503,12 @@ return; // return if no document loaded } - d = doc; // used with reload dialog - should not be null + d = doc (); // used with reload dialog - should not be null + + if (d == null) { + // no document loaded + return; + } } if (!doReload && !reloadDialogOpened) { @@ -1785,14 +1834,14 @@ * @param ev event describing the action */ public void insertUpdate(DocumentEvent ev) { - notifyModified (); + doNotifyModified (); } /** Gives notification that a portion of the document has been removed. * @param ev event describing the action */ public void removeUpdate(DocumentEvent ev) { - notifyModified (); + doNotifyModified (); } /** Listener to changes in the Env. @@ -1826,6 +1875,7 @@ /** Initialization of the document. */ public void run () { + StyledDocument doc = doc (); // synchronized (getLock ()) { /* Remove existing listener before running the loading task * This should prevent firing of insertUpdate() during load (or reload) @@ -2029,7 +2079,7 @@ super.redo(); if (saveTime == lastSaveTime) { - notifyUnmodified(); + doNotifyUnmodified(); } } @@ -2064,9 +2114,71 @@ super.undo(); if (saveTime == lastSaveTime) { - notifyUnmodified(); + doNotifyUnmodified(); } } + } + + /** Special reference holding the document. + */ + private final class DocReference extends java.lang.ref.SoftReference + implements Runnable { + /** hard reference to the document if marked as modified */ + private StyledDocument hard; + /** has finalization been run */ + private boolean finalized; + + public DocReference (StyledDocument doc) { + super (doc, org.openide.util.Utilities.activeReferenceQueue ()); + } + + /** Called to notify the reference that it should hold or not hard + * reference to the document. + * @param hard true if hard link should be hold + */ + public void hold (boolean hard) { + if (hard) { + this.hard = (StyledDocument)super.get (); + } else { + this.hard = null; + } + } + + public void run () { + closeDocument (); + synchronized (this) { + finalized = true; + notifyAll (); + } + } + + public Object get () { + throw new IllegalStateException ("Not implemented exception"); // NOI18N + } + + /** Gets the document. + * @param wait true whether to wait for finalization to finish or not + */ + public Object get (boolean wait) { + for (;;) { + Object ret = super.get (); + if (ret != null || !wait) { + return ret; + } + synchronized (this) { + if (!finalized) { + try { + wait (); + } catch (InterruptedException ex) { + // go on and try once more + } + } else { + return null; + } + } + } + } + } } Index: openide/src/org/openide/text/DocumentLine.java =================================================================== RCS file: /cvs/openide/src/org/openide/text/DocumentLine.java,v retrieving revision 1.52 diff -u -r1.52 DocumentLine.java --- openide/src/org/openide/text/DocumentLine.java 8 Dec 2003 18:35:45 -0000 1.52 +++ openide/src/org/openide/text/DocumentLine.java 7 Jan 2004 12:50:54 -0000 @@ -805,7 +805,7 @@ } Set (StyledDocument doc, CloneableEditorSupport support) { - listener = new LineListener (doc, support); + listener = new LineListener (support); } @@ -822,7 +822,7 @@ // revalidate all parts attached to this line // that they are still part of the line if(line instanceof DocumentLine) { - ((DocumentLine)line).notifyChange(p0, this, listener.doc); + ((DocumentLine)line).notifyChange(p0, this, listener.support.doc ()); } } } @@ -890,7 +890,7 @@ */ public Line getOriginal (int line) throws IndexOutOfBoundsException { int newLine = listener.getLine (line); - int offset = NbDocument.findLineOffset (listener.doc, newLine); + int offset = NbDocument.findLineOffset (listener.support.doc (), newLine); return safelyRegisterLine(createLine(offset)); } @@ -901,7 +901,7 @@ * @exception IndexOutOfBoundsException if line is invalid. */ public Line getCurrent (int line) throws IndexOutOfBoundsException { - int offset = NbDocument.findLineOffset (listener.doc, line); + int offset = NbDocument.findLineOffset (listener.support.doc (), line); return safelyRegisterLine(createLine(offset)); } @@ -929,7 +929,7 @@ } } DocumentRenderer renderer = new DocumentRenderer (); - listener.doc.render (renderer); + listener.support.doc ().render (renderer); return renderer.result; } } Index: openide/src/org/openide/text/LineListener.java =================================================================== RCS file: /cvs/openide/src/org/openide/text/LineListener.java,v retrieving revision 1.11 diff -u -r1.11 LineListener.java --- openide/src/org/openide/text/LineListener.java 12 Aug 2003 09:38:11 -0000 1.11 +++ openide/src/org/openide/text/LineListener.java 7 Jan 2004 12:50:54 -0000 @@ -24,8 +24,6 @@ implements javax.swing.event.DocumentListener { /** original count of lines */ private int orig; - /** document to work with */ - public final StyledDocument doc; /** root element of all lines */ private Element root; /** last tested amount of lines */ @@ -38,8 +36,9 @@ CloneableEditorSupport support; /** Creates new LineListener */ - public LineListener (StyledDocument doc, CloneableEditorSupport support) { - this.doc = doc; + public LineListener (CloneableEditorSupport support) { + StyledDocument doc = support.doc (); + this.struct = new LineStruct (); root = NbDocument.findLineRootElement (doc); orig = lines = root.getElementCount (); @@ -63,6 +62,7 @@ int delta = lines - elem; lines = elem; + StyledDocument doc = support.doc (); int lineNumber = NbDocument.findLineNumber (doc, p0.getOffset ()); if (delta > 0) { @@ -95,6 +95,7 @@ int delta = elem - lines; lines = elem; + StyledDocument doc = support.doc (); int lineNumber = NbDocument.findLineNumber (doc, p0.getOffset ()); if (delta > 0) { Index: openide/src/org/openide/text/PositionRef.java =================================================================== RCS file: /cvs/openide/src/org/openide/text/PositionRef.java,v retrieving revision 1.50 diff -u -r1.50 PositionRef.java --- openide/src/org/openide/text/PositionRef.java 30 Sep 2003 16:45:38 -0000 1.50 +++ openide/src/org/openide/text/PositionRef.java 7 Jan 2004 12:50:54 -0000 @@ -192,9 +192,6 @@ /** support for the editor */ transient private CloneableEditorSupport support; - /** the document for this manager or null if the manager is not in memory */ - transient private StyledDocument doc; - static final long serialVersionUID =-4374030124265110801L; /** Creates new manager * @param supp support to work with @@ -254,8 +251,6 @@ /** Converts all positions into document one. */ void documentOpened (StyledDocument doc) { - this.doc = doc; - processPositions(true); } @@ -264,8 +259,6 @@ */ void documentClosed () { processPositions(false); - - doc = null; } /** Puts/gets positions to/from memory. It also provides full @@ -502,7 +495,7 @@ "Illegal PositionKind: " + pos + "[offset=" // NOI18N + offset + ",line=" // NOI18N + line + ",column=" + column + "] in " // NOI18N - + doc + " used by " + support + "." // NOI18N + + support.doc() + " used by " + support + "." // NOI18N ); } @@ -555,7 +548,7 @@ "Illegal OutKind[offset=" // NOI18N + offset + ",line=" // NOI18N + line + ",column=" + column + "] in " // NOI18N - + doc + " used by " + support + "." // NOI18N + + support.doc () + " used by " + support + "." // NOI18N ); } @@ -594,7 +587,7 @@ "Illegal OutKind[offset=" // NOI18N + offset + ",line=" // NOI18N + line + ",column=" + column + "] in " // NOI18N - + doc + " used by " + support + "." // NOI18N + + support.doc () + " used by " + support + "." // NOI18N ); } @@ -616,7 +609,7 @@ if(offset < 0) { throw new IndexOutOfBoundsException( "Illegal OffsetKind[offset=" // NOI18N - + offset + "] in " + doc + " used by " // NOI18N + + offset + "] in " + support.doc () + " used by " // NOI18N + support + "." // NOI18N ); } @@ -650,7 +643,7 @@ if(offset < 0) { throw new IOException( "Illegal OffsetKind[offset=" // NOI18N - + offset + "] in " + doc + " used by " // NOI18N + + offset + "] in " + support.doc () + " used by " // NOI18N + support + "." // NOI18N ); } @@ -675,7 +668,7 @@ throw new IndexOutOfBoundsException( "Illegal LineKind[line=" // NOI18N + line + ",column=" + column + "] in " // NOI18N - + doc + " used by " + support + "." // NOI18N + + support.doc () + " used by " + support + "." // NOI18N ); } @@ -731,7 +724,7 @@ throw new IOException( "Illegal LineKind[line=" // NOI18N + line + ",column=" + column + "] in " // NOI18N - + doc + " used by " + support + "." // NOI18N + + support.doc () + " used by " + support + "." // NOI18N ); } @@ -856,6 +849,7 @@ } void render() { + StyledDocument doc = support.doc (false); if (doc != null) { doc.render(this); } else { @@ -902,6 +896,7 @@ } public void run() { + StyledDocument doc = support.doc (false); try { switch (opCode) { case KIND_TO_MEMORY: { Index: openide/test/unit/src/org/openide/text/CloneableEditorSupportTest.java =================================================================== RCS file: /cvs/openide/test/unit/src/org/openide/text/CloneableEditorSupportTest.java,v retrieving revision 1.5 diff -u -r1.5 CloneableEditorSupportTest.java --- openide/test/unit/src/org/openide/text/CloneableEditorSupportTest.java 18 Dec 2003 16:02:56 -0000 1.5 +++ openide/test/unit/src/org/openide/text/CloneableEditorSupportTest.java 7 Jan 2004 12:50:54 -0000 @@ -166,6 +166,45 @@ assertGC ("Document can dissapear", ref); } + public void testDocumentBeGarbageCollectedWhenNotModifiedButOpened () throws Exception { + content = "Ahoj\nMyDoc"; + javax.swing.text.Document doc = support.openDocument (); + assertNotNull (doc); + + java.lang.ref.WeakReference ref = new java.lang.ref.WeakReference (doc); + doc = null; + + assertGC ("Document can dissapear", ref); + + assertFalse ("Document is not loaded", support.isDocumentLoaded ()); + assertTrue ("Can be closed without problems", support.close ()); + } + + public void testDocumentIsNotGCedIfModified () throws Exception { + content = "Ahoj\nMyDoc"; + javax.swing.text.Document doc = support.openDocument (); + assertNotNull (doc); + doc.insertString (0, "Zmena", null); + + assertTrue ("Is modified", support.isModified ()); + + java.lang.ref.WeakReference ref = new java.lang.ref.WeakReference (doc); + doc = null; + + boolean ok; + try { + assertGC ("Should fail", ref); + ok = false; + } catch (AssertionFailedError expected) { + ok = true; + } + if (!ok) { + fail ("Document should not disappear, as it is modified"); + } + + assertTrue ("Document remains loaded", support.isDocumentLoaded ()); + + } private void compareStreamWithString(InputStream is, String s) throws Exception{ int i; Index: utilities/clisrc/org/netbeans/modules/openfile/cli/Handler.java =================================================================== RCS file: /cvs/utilities/clisrc/org/netbeans/modules/openfile/cli/Handler.java,v retrieving revision 1.3 diff -u -r1.3 Handler.java --- utilities/clisrc/org/netbeans/modules/openfile/cli/Handler.java 18 Sep 2003 16:28:27 -0000 1.3 +++ utilities/clisrc/org/netbeans/modules/openfile/cli/Handler.java 7 Jan 2004 12:51:42 -0000 @@ -35,80 +35,67 @@ return (Callback)Lookup.getDefault().lookup(Callback.class); } + private File findFile (File curDir, String name) { + File f = new File(name); + if (!f.isAbsolute()) { + f = new File(curDir, name); + } + return f; + } + + private int openFile (File curDir, CLIHandler.Args args, String[] argv, int i) { + String s = argv[i]; + if (s == null) { + log("Missing argument to --open", args); + return 2; + } + argv[i] = null; + Callback c = getCallback(); + if (c == null) { + // XXX I18N required for cmdline? + log("The User Utilities module must be installed for open-file functionality to work.", args); + return 2; + } + int line = -1; + File f = findFile (curDir, s); + if (!f.exists()) { + // Check if it is file:line syntax. + int idx = s.lastIndexOf(':'); // NOI18N + if (idx != -1) { + try { + line = Integer.parseInt(s.substring(idx + 1)) - 1; + f = findFile (curDir, s.substring(0, idx)); + } catch (NumberFormatException e) { + // OK, leave as a filename + } + } + } + // Just make sure it was opened, then exit. + boolean success = c.open(f, line, null); + return success ? 0 : 1; + } + protected int cli(CLIHandler.Args args) { String[] argv = args.getArguments(); File curDir = args.getCurrentDirectory (); - boolean wait = false; for (int i = 0; i < argv.length; i++) { if (argv[i] == null) { continue; } - if (argv[i].equals("-open")) { // NOI18N + if (argv[i].equals("--open") || argv[i].equals("-open")) { // NOI18N argv[i] = null; if (i == argv.length - 1) { - log("Missing argument to -open", args); + log("Missing argument to --open", args); return 2; } - String s = argv[++i]; - if (s == null) { - log("Missing argument to -open", args); - return 2; - } - argv[i] = null; - Callback c = getCallback(); - if (c == null) { - // XXX I18N required for cmdline? - log("The User Utilities module must be installed for open-file functionality to work.", args); - return 2; - } - int line = -1; - File f = new File(s); - if (!f.isAbsolute()) { - f = new File(curDir, s); - } - if (!f.exists()) { - // Check if it is file:line syntax. - int idx = s.lastIndexOf(':'); // NOI18N - if (idx != -1) { - try { - line = Integer.parseInt(s.substring(idx + 1)) - 1; - f = new File(s.substring(0, idx)); - } catch (NumberFormatException e) { - // OK, leave as a filename - } + i++; + while (i < argv.length && !argv[i].startsWith ("-")) { + int res = openFile (curDir, args, argv, i++); + if (res != 0) { + return res; } } - boolean success; - if (wait) { - // Open it, and wait for it to be closed. - Callback.Waiter w = new Callback.Waiter() { - public synchronized void done() { - notify(); - } - }; - success = c.open(f, line, w); - /* XXX uncomment when implemented in module - if (success) { - try { - synchronized (w) { - w.wait(); - } - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - */ - } else { - // Just make sure it was opened, then exit. - success = c.open(f, line, null); - } - if (!success) { - return 1; - } - } else if (argv[i].equals("-wait")) { // NOI18N - argv[i] = null; - wait = true; - } + } } // No problems. return 0; @@ -122,15 +109,9 @@ } protected void usage(PrintWriter w) { - w.println("-open FILE"); - w.println(" Open FILE."); - w.println("-open FILE:LINE"); - w.println(" Open FILE at line LINE (starting from 1)."); - /* XXX uncomment when implemented in module - w.println("-wait -open FILE[:LINE]"); - w.println(" Open FILE (maybe at line LINE), and wait until it is closed before exiting."); - w.println(" (Currently unimplemented.)"); - */ + w.println("Open File Module options:"); + w.println(" --open FILE open FILE."); + w.println(" --open FILE:LINE open FILE at line LINE (starting from 1)."); } }