From: <dal...@us...> - 2011-06-24 20:13:51
|
Revision: 19629 http://jedit.svn.sourceforge.net/jedit/?rev=19629&view=rev Author: daleanson Date: 2011-06-24 20:13:43 +0000 (Fri, 24 Jun 2011) Log Message: ----------- Updates for threading/performance improvements. Modified Paths: -------------- plugins/SideKick/trunk/sidekick/SideKick.java plugins/SideKick/trunk/sidekick/SideKickPlugin.java plugins/SideKick/trunk/sidekick/SideKickTree.java Modified: plugins/SideKick/trunk/sidekick/SideKick.java =================================================================== --- plugins/SideKick/trunk/sidekick/SideKick.java 2011-06-24 18:52:50 UTC (rev 19628) +++ plugins/SideKick/trunk/sidekick/SideKick.java 2011-06-24 20:13:43 UTC (rev 19629) @@ -28,13 +28,13 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import javax.swing.SwingWorker; import javax.swing.Timer; import javax.swing.tree.DefaultMutableTreeNode; import org.gjt.sp.jedit.Buffer; import org.gjt.sp.jedit.EditBus; import org.gjt.sp.jedit.EditPane; -import org.gjt.sp.jedit.Mode; import org.gjt.sp.jedit.View; import org.gjt.sp.jedit.jEdit; import org.gjt.sp.jedit.EditBus.EBHandler; @@ -181,12 +181,13 @@ sendUpdate(); } //}}} - DefaultErrorSource errorSource = new DefaultErrorSource("SideKick"); - SideKickParsedData[] data = new SideKickParsedData[1]; + ///DefaultErrorSource errorSource = new DefaultErrorSource("SideKick"); /// why is this here? Why not have ParseRequest create it? + ///SideKickParsedData[] data = new SideKickParsedData[1]; /// why is this an array? Why is this even here? - ParseRequest parseRequest = new ParseRequest( - parser, buffer, errorSource, data); - SideKickPlugin.execute(parseRequest); + ///ParseRequest parseRequest = new ParseRequest( + /// parser, buffer, errorSource, data); + ParseRequestWorker parseRequest = new ParseRequestWorker(parser, buffer); + SideKickPlugin.execute(view, parseRequest); } //}}} //{{{ dispose() method @@ -497,58 +498,46 @@ //{{{ Inner classes - - //{{{ ParseRequest class - class ParseRequest implements Runnable + + class ParseRequestWorker extends SwingWorker<SideKickParsedData, Object> { SideKickParser parser; Buffer buffer; DefaultErrorSource errorSource; - SideKickParsedData[] data; - ParseRequest(SideKickParser parser, Buffer buffer, - DefaultErrorSource errorSource, SideKickParsedData[] data) + ParseRequestWorker(SideKickParser parser, Buffer buffer) { this.parser = parser; this.buffer = buffer; - this.errorSource = errorSource; - this.data = data; + errorSource = new DefaultErrorSource("SideKick"); } - - public void run() + + @Override + public SideKickParsedData doInBackground() { + SideKickTree tree = (SideKickTree) view.getDockableWindowManager().getDockable("sidekick-tree"); + if (tree != null) + { + tree.showStopButton(true); + } buffer.readLock(); - data[0] = parser.parse(buffer,errorSource); + SideKickParsedData data = parser.parse(buffer, errorSource); buffer.readUnlock(); - buffer.setProperty(SideKickPlugin.PARSED_DATA_PROPERTY, data[0]); - EventQueue.invokeLater(new ParseAWTRequest( - parser, buffer, errorSource, data)); + return data; } - } //}}} - - //{{{ ParseAWTRequest class - class ParseAWTRequest implements Runnable - { - SideKickParser parser; - Buffer buffer; - SideKickParsedData[] data; - DefaultErrorSource errorSource; - - ParseAWTRequest(SideKickParser parser, Buffer buffer, - DefaultErrorSource errorSource, SideKickParsedData[] data) + + @Override + public void done() { - this.parser = parser; - this.buffer = buffer; - this.data = data; - this.errorSource = errorSource; - } - - public void run() - { try { - Log.log(Log.DEBUG,this,"ParseAWTRequest"); - + if (isCancelled()) + { + parser.stop(); + return; + } + SideKickParsedData data = get(); + setErrorSource(errorSource); int errorCount = errorSource.getErrorCount(); @@ -562,7 +551,7 @@ "sidekick.parsing-complete",pp)); } - buffer.setProperty(SideKickPlugin.PARSED_DATA_PROPERTY,data[0]); + buffer.setProperty(SideKickPlugin.PARSED_DATA_PROPERTY, data); if(buffer.getProperty("folding").equals("sidekick")) buffer.invalidateCachedFoldLevels(); @@ -570,18 +559,27 @@ while(_view != null) { if(_view.getBuffer() == buffer) - SideKickParsedData.setParsedData(_view,data[0]); + SideKickParsedData.setParsedData(_view, data); _view = _view.getNext(); } sendUpdate(); } + catch(Exception ie) + { + showNotParsedMessage(); + } finally { SideKickPlugin.finishParsingBuffer(buffer); + SideKickTree tree = (SideKickTree) view.getDockableWindowManager().getDockable("sidekick-tree"); + if (tree != null) + { + tree.showStopButton(false); + } } } - } //}}} + } // {{{ BufferChangeListener class /** Modified: plugins/SideKick/trunk/sidekick/SideKickPlugin.java =================================================================== --- plugins/SideKick/trunk/sidekick/SideKickPlugin.java 2011-06-24 18:52:50 UTC (rev 19628) +++ plugins/SideKick/trunk/sidekick/SideKickPlugin.java 2011-06-24 20:13:43 UTC (rev 19629) @@ -37,6 +37,7 @@ import java.util.Set; import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import javax.swing.SwingWorker; //}}} /** @@ -70,6 +71,7 @@ private static Set<Buffer> parsedBufferSet; private static Map<View, SideKickToolBar> toolBars; private static boolean toolBarsEnabled; + private static Map<View, SwingWorker> workers; //{{{ start() method public void start() @@ -77,6 +79,7 @@ BeanShell.getNameSpace().addCommandPath(MACRO_PATH, getClass()); sidekicks = new HashMap<View, SideKick>(); parsers = new HashMap<String, SideKickParser>(); + workers = new HashMap<View, SwingWorker>(); parsedBufferSet = new HashSet<Buffer>(); toolBars = new HashMap<View, SideKickToolBar>(); toolBarsEnabled = jEdit.getBooleanProperty(SHOW_TOOL_BAR); @@ -283,6 +286,14 @@ } executor.execute(runnable); } + + public static void execute(View view, SwingWorker worker) + { + // TODO: there should be only one worker per view. Is it possible + // there could be more than one? + workers.put(view, worker); + worker.execute(); + } //{{{ isParsingBuffer() public static boolean isParsingBuffer(Buffer buffer) @@ -374,9 +385,10 @@ } //}}} public static void stop(View view) { - SideKick sidekick = sidekicks.get(view); - if (sidekick == null) return; - sidekick.getParser().stop(); + SwingWorker worker = workers.get(view); + if (worker != null && !worker.isCancelled() && !worker.isDone()) { + worker.cancel(true); + } } //}}} Modified: plugins/SideKick/trunk/sidekick/SideKickTree.java =================================================================== --- plugins/SideKick/trunk/sidekick/SideKickTree.java 2011-06-24 18:52:50 UTC (rev 19628) +++ plugins/SideKick/trunk/sidekick/SideKickTree.java 2011-06-24 20:13:43 UTC (rev 19629) @@ -18,11 +18,11 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ +*/ package sidekick; -//{{{ Imports +// {{{ Imports import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; @@ -52,6 +52,7 @@ import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.SwingUtilities; +import javax.swing.SwingWorker; import javax.swing.JToolBar; import javax.swing.JTree; import javax.swing.Timer; @@ -88,7 +89,7 @@ import org.gjt.sp.util.Log; import org.gjt.sp.util.StandardUtilities; import org.gjt.sp.util.StringList; -//}}} +// }}} /** * The Structure Browser dockable. One instance is created for each View. @@ -96,1203 +97,1397 @@ public class SideKickTree extends JPanel implements DefaultFocusComponent { - //{{{ Instance variables - private RolloverButton parseBtn; - private RolloverButton propsBtn; - private RolloverButton stopBtn; + // {{{ Instance variables + private RolloverButton parseBtn; + private RolloverButton propsBtn; + private RolloverButton stopBtn; - private JComboBox parserCombo; - protected JTree tree; - //protected JEditorPane status; - protected JTextArea status; - private JPanel topPanel; - private JSplitPane splitter; - private boolean statusShowing = false; - private Buffer lastParsedBuffer = null; - private JToolBar filterBox; - protected JPopupMenu configMenu; - protected JCheckBoxMenuItem onChange; - protected JCheckBoxMenuItem followCaret; - protected JCheckBoxMenuItem onSave; + private JComboBox parserCombo; + protected JTree tree; + // protected JEditorPane status; + protected JTextArea status; + private JPanel topPanel; + private JSplitPane splitter; + private boolean statusShowing = false; + private Buffer lastParsedBuffer = null; + private JToolBar filterBox; + protected JPopupMenu configMenu; + protected JCheckBoxMenuItem onChange; + protected JCheckBoxMenuItem followCaret; + protected JCheckBoxMenuItem onSave; - protected View view; - private Timer caretTimer; + protected View view; + private Timer caretTimer; - protected SideKickParsedData data; + protected SideKickParsedData data; - private int autoExpandTree = 0; - private JPanel toolBox; - private JPanel parserPanel = null; + private int autoExpandTree = 0; + private JPanel toolBox; + private JPanel parserPanel = null; - private JLabel search; - private JTextField searchField; - private JButton clearSearchBtn; - //}}} + private JLabel search; + private JTextField searchField; + private JButton clearSearchBtn; + // }}} - //{{{ SideKickTree constructor - public SideKickTree(View view, boolean docked) - { - super(new BorderLayout()); + // {{{ SideKickTree constructor + public SideKickTree(View view, boolean docked) + { + super(new BorderLayout()); - this.view = view; + this.view = view; - topPanel = new JPanel(new BorderLayout()); + topPanel = new JPanel(new BorderLayout()); - // create toolbar with parse button - JToolBar buttonBox = new JToolBar(); - buttonBox.setFloatable(false); - filterBox = new JToolBar(); - filterBox.setLayout(new BorderLayout()); - filterBox.setFloatable(false); + // create toolbar with parse button + JToolBar buttonBox = new JToolBar(); + buttonBox.setFloatable(false); + filterBox = new JToolBar(); + filterBox.setLayout(new BorderLayout()); + filterBox.setFloatable(false); - parseBtn = new RolloverButton(GUIUtilities.loadIcon("Parse.png")); + parseBtn = new RolloverButton(GUIUtilities.loadIcon("Parse.png")); - parseBtn.setToolTipText(jEdit.getProperty("sidekick-tree.parse")); - parseBtn.setMargin(new Insets(0,0,0,0)); - parseBtn.setRequestFocusEnabled(false); - parseBtn.setEnabled(true); - ActionListener ah = new ActionHandler(); - parseBtn.addActionListener(ah); + parseBtn.setToolTipText(jEdit.getProperty("sidekick-tree.parse")); + parseBtn.setMargin(new Insets(0,0,0,0)); + parseBtn.setRequestFocusEnabled(false); + parseBtn.setEnabled(true); + ActionListener ah = new ActionHandler(); + parseBtn.addActionListener(ah); - propsBtn= new RolloverButton(GUIUtilities.loadIcon("ButtonProperties.png")); - propsBtn.setToolTipText(jEdit.getProperty("sidekick-tree.mode-options")); - propsBtn.addActionListener(new SideKickProperties()); + propsBtn = new RolloverButton(GUIUtilities.loadIcon("ButtonProperties.png")); + propsBtn.setToolTipText(jEdit.getProperty("sidekick-tree.mode-options")); + propsBtn.addActionListener(new SideKickProperties()); - stopBtn = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty( - "hypersearch-results.stop.icon"))); - stopBtn.setToolTipText(jEdit.getProperty("sidekick-tree.stop-parsing")); - stopBtn.addActionListener(ah); - - configMenu = new JPopupMenu("Parse"); - followCaret = new JCheckBoxMenuItem("Follow Caret"); + stopBtn = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty("hypersearch-results.stop.icon"))); + stopBtn.setToolTipText(jEdit.getProperty("sidekick-tree.stop-parsing")); + stopBtn.addActionListener(ah); - configMenu.add(followCaret); -// configMenu = new PopupMenu("Parse on..."); - JMenuItem item = new JMenuItem("Parse on..."); - item.setEnabled(false); - configMenu.add(item); + configMenu = new JPopupMenu("Parse"); + followCaret = new JCheckBoxMenuItem("Follow Caret"); - onChange = new JCheckBoxMenuItem("Buffer change"); - onChange.setState(SideKick.isParseOnChange()); - onSave = new JCheckBoxMenuItem("Buffer save"); - onSave.setState(SideKick.isParseOnSave()); - configMenu.add(onChange); - configMenu.add(onSave); - parseBtn.setComponentPopupMenu(configMenu); - onChange.addActionListener(ah); - onSave.addActionListener(ah); - followCaret.addActionListener(ah); - search = new JLabel(jEdit.getProperty("sidekick-tree.filter.label") + " "); - searchField = new JTextField(); - searchField.setToolTipText(jEdit.getProperty("sidekick-tree.filter.tooltip")); - clearSearchBtn = new RolloverButton(GUIUtilities.loadIcon( "22x22/actions/edit-clear.png" )); - clearSearchBtn.addActionListener( - new ActionListener() { - public void actionPerformed( ActionEvent ae ) { - searchField.setText(""); - updateFilter(); - } - } - ); - clearSearchBtn.setToolTipText(jEdit.getProperty("sidekick-tree.clear-filter.tooltip")); + configMenu.add(followCaret); + // configMenu = new PopupMenu("Parse on..."); + JMenuItem item = new JMenuItem("Parse on..."); + item.setEnabled(false); + configMenu.add(item); - buttonBox.add(parseBtn); - buttonBox.add(propsBtn); - buttonBox.add(stopBtn); - filterBox.add(search, BorderLayout.WEST); - filterBox.add(searchField, BorderLayout.CENTER); - filterBox.add(clearSearchBtn, BorderLayout.EAST); + onChange = new JCheckBoxMenuItem("Buffer change"); + onChange.setState(SideKick.isParseOnChange()); + onSave = new JCheckBoxMenuItem("Buffer save"); + onSave.setState(SideKick.isParseOnSave()); + configMenu.add(onChange); + configMenu.add(onSave); + parseBtn.setComponentPopupMenu(configMenu); + onChange.addActionListener(ah); + onSave.addActionListener(ah); + followCaret.addActionListener(ah); + search = new JLabel(jEdit.getProperty("sidekick-tree.filter.label") + " "); + searchField = new JTextField(); + searchField.setToolTipText(jEdit.getProperty("sidekick-tree.filter.tooltip")); + clearSearchBtn = new RolloverButton(GUIUtilities.loadIcon("22x22/actions/edit-clear.png")); + clearSearchBtn.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent ae) + { + searchField.setText(""); + updateFilter(); + } + } + ); + clearSearchBtn.setToolTipText(jEdit.getProperty("sidekick-tree.clear-filter.tooltip")); - buttonBox.add(Box.createGlue()); + buttonBox.add(parseBtn); + buttonBox.add(propsBtn); + buttonBox.add(stopBtn); + filterBox.add(search, BorderLayout.WEST); + filterBox.add(searchField, BorderLayout.CENTER); + filterBox.add(clearSearchBtn, BorderLayout.EAST); - parserCombo = new JComboBox(); - reloadParserCombo(); - parserCombo.setToolTipText(jEdit.getProperty("sidekick-tree.parsercombo.tooltip")); + buttonBox.add(Box.createGlue()); - buttonBox.add(parserCombo); - parserCombo.addActionListener(ah); - parserCombo.addActionListener( - new ActionListener() { - public void actionPerformed( ActionEvent ae ) { - searchField.setText(""); - updateFilter(); - } - } - ); + parserCombo = new JComboBox(); + reloadParserCombo(); + parserCombo.setToolTipText(jEdit.getProperty("sidekick-tree.parsercombo.tooltip")); - toolBox = new JPanel(new BorderLayout()); - toolBox.add(BorderLayout.NORTH, buttonBox); - toolBox.add(BorderLayout.SOUTH, filterBox); + buttonBox.add(parserCombo); + parserCombo.addActionListener(ah); + parserCombo.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent ae) + { + searchField.setText(""); + updateFilter(); + } + } + ); - topPanel.add(BorderLayout.NORTH,toolBox); + toolBox = new JPanel(new BorderLayout()); + toolBox.add(BorderLayout.NORTH, buttonBox); + toolBox.add(BorderLayout.SOUTH, filterBox); - // create a faux model that will do until a real one arrives - TreeModel emptyModel = new DefaultTreeModel(new DefaultMutableTreeNode(null)); - emptyModel = new FilteredTreeModel((DefaultTreeModel)emptyModel, true); - tree = buildTree(emptyModel); - tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); - KeyHandler kh = new KeyHandler(); - tree.addKeyListener(kh); - if(docked) - tree.addMouseMotionListener(new MouseHandler()); - searchField.addKeyListener(kh); + topPanel.add(BorderLayout.NORTH, toolBox); - // looks bad with the OS X L&F, apparently... - if(!OperatingSystem.isMacOSLF()) - tree.putClientProperty("JTree.lineStyle", "Angled"); + // create a faux model that will do until a real one arrives + TreeModel emptyModel = new DefaultTreeModel(new DefaultMutableTreeNode(null)); + emptyModel = new FilteredTreeModel((DefaultTreeModel) emptyModel, true); + tree = buildTree(emptyModel); + tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); + KeyHandler kh = new KeyHandler(); + tree.addKeyListener(kh); + if (docked) + { + tree.addMouseMotionListener(new MouseHandler()); + } + searchField.addKeyListener(kh); - tree.setVisibleRowCount(10); - tree.setCellRenderer(new Renderer()); + // looks bad with the OS X L&F, apparently... + if (!OperatingSystem.isMacOSLF()) + { + tree.putClientProperty("JTree.lineStyle", "Angled"); + } - topPanel.add(BorderLayout.CENTER,new JScrollPane(tree)); + tree.setVisibleRowCount(10); + tree.setCellRenderer(new Renderer()); - status = new JTextArea(); - //status.setContentType("text/html"); - status.setEditable(false); - status.setBackground(jEdit.getColorProperty("view.bgColor")); - status.setForeground(jEdit.getColorProperty("view.fgColor")); - status.setFont(view.getEditPane().getTextArea().getPainter().getFont()); - JScrollPane status_scroller = new JScrollPane(status); + topPanel.add(BorderLayout.CENTER, new JScrollPane(tree)); - splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, topPanel, status_scroller); + status = new JTextArea(); + // status.setContentType("text/html"); + status.setEditable(false); + status.setBackground(jEdit.getColorProperty("view.bgColor")); + status.setForeground(jEdit.getColorProperty("view.fgColor")); + status.setFont(view.getEditPane().getTextArea().getPainter().getFont()); + JScrollPane status_scroller = new JScrollPane(status); - status_scroller.setMinimumSize(new Dimension(0, 30)); - splitter.setOneTouchExpandable(true); - splitter.setResizeWeight(1.0f); - int location =splitter.getSize().height - splitter.getInsets().bottom - - splitter.getDividerSize() - status_scroller.getMinimumSize().height; - location = jEdit.getIntegerProperty("sidekick.splitter.location", location); + splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, topPanel, status_scroller); + status_scroller.setMinimumSize(new Dimension(0, 30)); + splitter.setOneTouchExpandable(true); + splitter.setResizeWeight(1.0f); + int location = splitter.getSize().height - splitter.getInsets().bottom - splitter.getDividerSize() - status_scroller.getMinimumSize().height; + location = jEdit.getIntegerProperty("sidekick.splitter.location", location); - splitter.setDividerLocation(location); + splitter.setDividerLocation(location); - //add(splitter); + // add(splitter); - propertiesChanged(); + propertiesChanged(); - CaretHandler caretListener = new CaretHandler(); + CaretHandler caretListener = new CaretHandler(); - EditPane[] editPanes = view.getEditPanes(); - for(int i = 0; i < editPanes.length; i++) - { - JEditTextArea textArea = editPanes[i].getTextArea(); - textArea.putClientProperty(CaretHandler.class, caretListener); - textArea.addCaretListener(caretListener); - } + EditPane[] editPanes = view.getEditPanes(); + for (int i = 0; i < editPanes.length; i++) + { + JEditTextArea textArea = editPanes[i].getTextArea(); + textArea.putClientProperty(CaretHandler.class, caretListener); + textArea.addCaretListener(caretListener); + } - update(); - } //}}} + update(); + } // }}} - //{{{ focusOnDefaultComponent() method - public void focusOnDefaultComponent() - { - searchField.requestFocusInWindow(); - } //}}} + public void showStopButton(final boolean show) + { + SwingUtilities.invokeLater (new Runnable() + { + public void run() + { + stopBtn.setVisible(show); + } + } + ); + } - //{{{ addNotify() method - public void addNotify() - { - super.addNotify(); - EditBus.addToBus(this); - } //}}} + // {{{ focusOnDefaultComponent() method + public void focusOnDefaultComponent() + { + searchField.requestFocusInWindow(); + } // }}} - //{{{ removeNotify() method - public void removeNotify() - { - super.removeNotify(); - EditBus.removeFromBus(this); - } //}}} - - //{{{ selectPath() method - protected void selectPath(TreePath path) - { - tree.setSelectionPath(path); - Rectangle r = tree.getPathBounds(path); - if (r != null) { - r.width = 1; - tree.scrollRectToVisible(r); - } - } //}}} + // {{{ addNotify() method + public void addNotify() + { + super.addNotify(); + EditBus.addToBus(this); + } // }}} - //{{{ handleEditPaneUpdate() method - @EBHandler - public void handleEditPaneUpdate(EditPaneUpdate epu) - { - EditPane editPane = epu.getEditPane(); - if(epu.getWhat() == EditPaneUpdate.CREATED) - editPane.getTextArea().addCaretListener(new CaretHandler()); - } //}}} + // {{{ removeNotify() method + public void removeNotify() + { + super.removeNotify(); + EditBus.removeFromBus(this); + } // }}} - //{{{ handlePropertiesChanged method - @EBHandler - public void handlePropertiesChanged(PropertiesChanged msg) - { - propertiesChanged(); - } //}}} + // {{{ selectPath() method + protected void selectPath(TreePath path) + { + tree.setSelectionPath(path); + Rectangle r = tree.getPathBounds(path); + if (r != null) + { + r.width = 1; + tree.scrollRectToVisible(r); + } + } // }}} - //{{{ handleSideKickUpdate() method - @EBHandler - public void handleSideKickUpdate(SideKickUpdate msg) - { - if(msg.getView() == view) - update(); - } //}}} + // {{{ handleEditPaneUpdate() method + @EBHandler + public void handleEditPaneUpdate(EditPaneUpdate epu) + { + EditPane editPane = epu.getEditPane(); + if (epu.getWhat() == EditPaneUpdate.CREATED) + { + editPane.getTextArea().addCaretListener(new CaretHandler()); + } + } // }}} - //{{{ setStatus() method - public void setStatus(String msg) { - status.setText(msg); - }// }}} + // {{{ handlePropertiesChanged method + @EBHandler + public void handlePropertiesChanged(PropertiesChanged msg) + { + propertiesChanged(); + } // }}} - //{{{ addData method - protected void addData(Object obj, Stack<String> keys) - { - if (obj instanceof DefaultMutableTreeNode) { - DefaultMutableTreeNode node = (DefaultMutableTreeNode)obj; - String cur_key = ""; - FilteredTreeModel model = (FilteredTreeModel)tree.getModel(); - try { - Asset a = (Asset)node.getUserObject(); - if (a != null) - cur_key = a.getName(); - } catch (ClassCastException ex) { - if (node.toString() != null) - cur_key = node.toString(); - } - keys.push(cur_key); - if (model.isLeaf(node)) { - for (String key : keys) { - model.addSearchKey(node, key); - } - } + // {{{ handleSideKickUpdate() method + @EBHandler + public void handleSideKickUpdate(SideKickUpdate msg) + { + if (msg.getView() == view) + { + update(); + } + } // }}} - Enumeration<DefaultMutableTreeNode> e; - for (e = node.children(); e.hasMoreElements(); ) { - addData(e.nextElement(), keys); - } + // {{{ setStatus() method + public void setStatus(String msg) + { + status.setText(msg); + } // }}} - keys.pop(); - } else { - Log.log(Log.DEBUG, this, "addData called on a node that isn't a treenode!!!!!!!!!"); // how exciting! - } - } //}}} + // {{{ addData method + protected void addData(Object obj, Stack<String> keys) + { + if (obj instanceof DefaultMutableTreeNode) + { + DefaultMutableTreeNode node = (DefaultMutableTreeNode) obj; + String cur_key = ""; + FilteredTreeModel model = (FilteredTreeModel) tree.getModel(); + try + { + Asset a = (Asset) node.getUserObject(); + if (a != null) + { + cur_key = a.getName(); + } + } + catch (ClassCastException ex) + { + if (node.toString() != null) + { + cur_key = node.toString(); + } + } + keys.push(cur_key); + if (model.isLeaf(node)) + { + for (String key : keys) + { + model.addSearchKey(node, key); + } + } - //{{{ updateSearchData() method - protected void updateSearchData() - { - DefaultMutableTreeNode root; - FilteredTreeModel model = (FilteredTreeModel)tree.getModel(); - root = (DefaultMutableTreeNode)model.getRoot(); - addData(root, new Stack<String>()); - } //}}} + Enumeration<DefaultMutableTreeNode> e; + for (e = node.children(); e.hasMoreElements();) + { + addData(e.nextElement(), keys); + } - //{{{ update() method - protected void update() - { - onChange.setState(SideKick.isParseOnChange()); - onSave.setState(SideKick.isParseOnSave()); - Buffer parsedBuffer = view.getBuffer(); - SideKickParser parser = SideKickPlugin.getParserForBuffer(parsedBuffer); - if (parser != null) { - Object item = parserCombo.getSelectedItem(); - if (item != parser.getName()) { - parserCombo.setSelectedItem(parser.getName()); - } - } + keys.pop(); + } + else + { + Log.log(Log.DEBUG, this, "addData called on a node that isn't a treenode!!!!!!!!!"); // how exciting! + } + } // }}} - data = SideKickParsedData.getParsedData(view); - if(parser == null || data == null) - { - DefaultMutableTreeNode root = new DefaultMutableTreeNode(parsedBuffer.getName()); - root.insert(new DefaultMutableTreeNode( - jEdit.getProperty("sidekick-tree.not-parsed")),0); - tree.setModel(new FilteredTreeModel(new DefaultTreeModel(root), true)); - lastParsedBuffer = null; - } - else - { - tree.setModel(new FilteredTreeModel(data.tree, true)); - lastParsedBuffer = parsedBuffer; - if(SideKick.isFollowCaret()) - expandTreeAt(view.getTextArea().getCaretPosition()); - } - updateSearchData(); + // {{{ updateSearchData() method + protected void updateSearchData() + { + DefaultMutableTreeNode root; + FilteredTreeModel model = (FilteredTreeModel) tree.getModel(); + root = (DefaultMutableTreeNode) model.getRoot(); + addData(root, new Stack<String>()); + } // }}} - if (data != null && data.expansionModel != null) - { - // collapse all rows, then expand per the expansion model - for (int i = tree.getRowCount() - 1; i >= 0; i--) - { - tree.collapseRow(i); - } - for (Integer row : data.expansionModel) - { - tree.expandRow(row); - } - } - else - { - if (autoExpandTree == -1) - expandAll(true); - else if (autoExpandTree == 0) - expandAll(false); - else if (autoExpandTree > 0) { - tree.expandRow( 0 ); - for (int i = 1; i < autoExpandTree; i++) { - for ( int j = tree.getRowCount() - 1; j > 0; j-- ) - tree.expandRow( j ); - } - } - } + // {{{ update() method + protected void update() + { + onChange.setState(SideKick.isParseOnChange()); + onSave.setState(SideKick.isParseOnSave()); + Buffer parsedBuffer = view.getBuffer(); + SideKickParser parser = SideKickPlugin.getParserForBuffer(parsedBuffer); + if (parser != null) + { + Object item = parserCombo.getSelectedItem(); + if (item != parser.getName()) + { + parserCombo.setSelectedItem(parser.getName()); + } + } - if (searchField.getText().length() != 0) - updateFilter(); + data = SideKickParsedData.getParsedData(view); + if (parser == null || data == null) + { + DefaultMutableTreeNode root = new DefaultMutableTreeNode(parsedBuffer.getName()); + root.insert(new DefaultMutableTreeNode(jEdit.getProperty("sidekick-tree.not-parsed")),0); + tree.setModel(new FilteredTreeModel(new DefaultTreeModel(root), true)); + lastParsedBuffer = null; + } + else + { + tree.setModel(new FilteredTreeModel(data.tree, true)); + lastParsedBuffer = parsedBuffer; + if (SideKick.isFollowCaret()) + { + expandTreeAt(view.getTextArea().getCaretPosition()); + } + } + updateSearchData(); - } //}}} + if (data != null && data.expansionModel != null) + { + // collapse all rows, then expand per the expansion model + for (int i = tree.getRowCount() - 1; i >= 0; i--) + { + tree.collapseRow(i); + } + for (Integer row : data.expansionModel) + { + tree.expandRow(row); + } + } + else + { + if (autoExpandTree == -1) + { + expandAll(true); + } + else if (autoExpandTree == 0) + { + expandAll(false); + } + else if (autoExpandTree > 0) + { + tree.expandRow(0); + for (int i = 1; i < autoExpandTree; i++) + { + for (int j = tree.getRowCount() - 1; j > 0; j--) + { + tree.expandRow(j); + } - //{{{ expandAll() methods - /** - * Expand or collapse all nodes in the tree. - * @param expand if true, expand all nodes, if false, collapse all nodes - */ - public void expandAll( boolean expand ) { - TreeNode root = ( TreeNode ) tree.getModel().getRoot(); - expandAll( new TreePath( root ), expand ); - } + } + } + } - // recursive method to traverse children - private void expandAll( TreePath parent, boolean expand ) { - TreeNode node = ( TreeNode ) parent.getLastPathComponent(); - if ( node.getChildCount() >= 0 ) { - for ( Enumeration e = node.children(); e.hasMoreElements(); ) { - TreeNode n = ( TreeNode ) e.nextElement(); - TreePath path = parent.pathByAddingChild( n ); - expandAll( path, expand ); - } - } + if (searchField.getText().length() != 0) + { + updateFilter(); + } - // expansion or collapse must be done from the bottom up - if ( expand ) { - tree.expandPath( parent ); - } - else { - tree.collapsePath( parent ); - } - }//}}} + } // }}} - //{{{ buildTree() method - protected JTree buildTree(TreeModel model) - { - return new CustomTree(model); - }//}}} + // {{{ expandAll() methods + /** + * Expand or collapse all nodes in the tree. + * @param expand if true, expand all nodes, if false, collapse all nodes + */ + public void expandAll(boolean expand) + { + TreeNode root = (TreeNode) tree.getModel().getRoot(); + expandAll(new TreePath(root), expand); + } - //{{{ buildActionListener() method - /** - * Creates an action listener for the parse button. - */ - protected ActionListener buildActionListener() - { - return new ActionHandler(); - }//}}} + // recursive method to traverse children + private void expandAll(final TreePath parent, final boolean expand) + { + SwingUtilities.invokeLater(new Runnable() + { + public void run() + { + TreeNode node = (TreeNode) parent.getLastPathComponent(); + if (node.getChildCount() >= 0) + { + for (Enumeration e = node.children(); e.hasMoreElements();) + { + TreeNode n = (TreeNode) e.nextElement(); + TreePath path = parent.pathByAddingChild(n); + expandAll(path, expand); + } + } - //{{{ propertiesChanged() method - protected void propertiesChanged() - { - followCaret.setSelected(SideKick.isFollowCaret()); - Mode m = view.getBuffer().getMode(); - String mode = m != null? m.getName(): null; - autoExpandTree = AbstractModeOptionPane.getIntegerProperty(mode, SideKick.AUTO_EXPAND_DEPTH, 1); - // autoExpandTree = ModeOptions.getAutoExpandTreeDepth(); + // expansion or collapse must be done from the bottom up + if (expand) + { + tree.expandPath(parent); + } + else + { + tree.collapsePath(parent); + } + } + } ); + } // }}} - if (AbstractModeOptionPane.getBooleanProperty(mode, SideKick.SHOW_STATUS)) { - if (!statusShowing) { - remove(topPanel); - splitter.setTopComponent(topPanel); - add(splitter); - } - statusShowing = true; - } - else { - remove(splitter); - splitter.remove(topPanel); - add(topPanel); - statusShowing = false; - } - - // show or hide the filter box - final boolean showFilter = jEdit.getBooleanProperty(SideKick.SHOW_FILTER, true); - if (showFilter != filterBox.isVisible()) { - SwingUtilities.invokeLater(new Runnable() - { - public void run() - { - filterBox.setVisible(showFilter); - searchField.setEnabled(showFilter); - } - }); - } - } //}}} + // {{{ buildTree() method + protected JTree buildTree(TreeModel model) + { + return new CustomTree(model); + } // }}} - //{{{ parserList() method - /** @return a list of parsers, sorted, with special choices - on top */ - public static StringList parserList() { - String[] serviceNames = ServiceManager.getServiceNames(SideKickParser.SERVICE); - Arrays.sort(serviceNames, new StandardUtilities.StringCompare<String>(true)); - StringList sl = new StringList(); - sl.add(SideKickPlugin.NONE); - sl.add(SideKickPlugin.DEFAULT); - sl.addAll(serviceNames); - return sl; - } // }}} + // {{{ buildActionListener() method + /** + * Creates an action listener for the parse button. + */ + protected ActionListener buildActionListener() + { + return new ActionHandler(); + } // }}} - //{{{ reloadParserCombo() method - void reloadParserCombo() - { - parserCombo.setModel(new DefaultComboBoxModel(parserList().toArray())); - SideKickParser currentParser = SideKickPlugin.getParserForBuffer(view.getBuffer()); - if (currentParser != null ) - { - String name = currentParser.getName(); - name = name == null ? SideKickPlugin.DEFAULT : name; - parserCombo.setSelectedItem(name); - } - else - { - String pp = view.getBuffer().getStringProperty(SideKickPlugin.PARSER_PROPERTY); - if (pp == SideKickPlugin.NONE) parserCombo.setSelectedItem(SideKickPlugin.NONE); - else parserCombo.setSelectedItem(SideKickPlugin.DEFAULT); - } - } // }}} + // {{{ propertiesChanged() method + protected void propertiesChanged() + { + followCaret.setSelected(SideKick.isFollowCaret()); + Mode m = view.getBuffer().getMode(); + String mode = m != null ? m.getName() : null; + autoExpandTree = AbstractModeOptionPane.getIntegerProperty(mode, SideKick.AUTO_EXPAND_DEPTH, 1); + // autoExpandTree = ModeOptions.getAutoExpandTreeDepth(); - //{{{ addParserPanel() method - void addParserPanel(SideKickParser parser) - { - JPanel newParserPanel = parser.getPanel(); - boolean returnFocusToSearchField = searchField.hasFocus(); - if (newParserPanel != parserPanel) - { - if (parserPanel != null) - { - toolBox.remove(parserPanel); - parserPanel = null; - } - if (newParserPanel != null) - { - toolBox.add(BorderLayout.WEST,newParserPanel); - parserPanel = newParserPanel; - } - } - if (returnFocusToSearchField) - focusOnDefaultComponent(); - } // }}} + if (AbstractModeOptionPane.getBooleanProperty(mode, SideKick.SHOW_STATUS)) + { + if (!statusShowing) + { + remove(topPanel); + splitter.setTopComponent(topPanel); + add(splitter); + } + statusShowing = true; + } + else + { + remove(splitter); + splitter.remove(topPanel); + add(topPanel); + statusShowing = false; + } - //{{{ removeParserPanel() method - void removeParserPanel() - { - if (parserPanel != null) - { - toolBox.remove(parserPanel); - parserPanel = null; - } - } // }}} + // show or hide the filter box + final boolean showFilter = jEdit.getBooleanProperty(SideKick.SHOW_FILTER, true); + if (showFilter != filterBox.isVisible()) + { + SwingUtilities.invokeLater(new Runnable() + { + public void run() + { + filterBox.setVisible(showFilter); + searchField.setEnabled(showFilter); + } + } ); + } + } // }}} - //{{{ expandTreeWithDelay() method - /** - * Expands the tree after a delay. - * The delay timer is restarted each time this method is called. - */ - protected void expandTreeWithDelay() - { - if(caretTimer != null) { - caretTimer.stop(); - } - else - { - caretTimer = new Timer(0,new ActionListener() - { - public void actionPerformed(ActionEvent evt) - { - // If the filter is *not* persistent, then clear - // it when the tree is expanded for the current - // caret position. - if (!jEdit.getBooleanProperty("sidekick.persistentFilter")) { - if (searchField.getText().length() > 0) { - searchField.setText(""); - updateFilter(false); - } - } - TextArea textArea = view.getTextArea(); - int caret = textArea.getCaretPosition(); - Selection s = textArea.getSelectionAtOffset(caret); - expandTreeAt(s == null ? caret : s.getStart()); - } - }); - caretTimer.setInitialDelay(500); - caretTimer.setRepeats(false); - } - caretTimer.start(); - } //}}} + // {{{ parserList() method + /** @return a list of parsers, sorted, with special choices + * on top */ + public static StringList parserList() + { + String[] serviceNames = ServiceManager.getServiceNames(SideKickParser.SERVICE); + Arrays.sort(serviceNames, new StandardUtilities.StringCompare<String>(true)); + StringList sl = new StringList(); + sl.add(SideKickPlugin.NONE); + sl.add(SideKickPlugin.DEFAULT); + sl.addAll(serviceNames); + return sl; + } // }}} - //{{{ expandTreeAt() method - protected void expandTreeAt(int dot) - { - if(data == null) - return; + // {{{ reloadParserCombo() method + void reloadParserCombo() + { + parserCombo.setModel(new DefaultComboBoxModel(parserList().toArray())); + SideKickParser currentParser = SideKickPlugin.getParserForBuffer(view.getBuffer()); + if (currentParser != null) + { + String name = currentParser.getName(); + name = name == null ? SideKickPlugin.DEFAULT : name; + parserCombo.setSelectedItem(name); + } + else + { + String pp = view.getBuffer().getStringProperty(SideKickPlugin.PARSER_PROPERTY); + if (pp == SideKickPlugin.NONE) + { + parserCombo.setSelectedItem(SideKickPlugin.NONE); + } + else + { + parserCombo.setSelectedItem(SideKickPlugin.DEFAULT); + } + } + } // }}} - TreePath treePath = data.getTreePathForPosition(dot); - if(treePath != null) - { - tree.expandPath(treePath); - tree.setSelectionPath(treePath); - if (jEdit.getBooleanProperty("sidekick.scrollToVisible")) { - Rectangle r = tree.getPathBounds(treePath); - if (r != null) { - r.width = 1; - tree.scrollRectToVisible(r); - } else - tree.scrollPathToVisible(treePath); - } - } - } //}}} - - protected void expandCurrentNode() { - DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); - TreePath path = new TreePath(node.getPath()); - tree.expandPath(path); - } - - protected void collapseCurrentNode() { - DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); - TreePath path = new TreePath(node.getPath()); - tree.collapsePath(path); - } + // {{{ addParserPanel() method + void addParserPanel(SideKickParser parser) + { + JPanel newParserPanel = parser.getPanel(); + boolean returnFocusToSearchField = searchField.hasFocus(); + if (newParserPanel != parserPanel) + { + if (parserPanel != null) + { + toolBox.remove(parserPanel); + parserPanel = null; + } + if (newParserPanel != null) + { + toolBox.add(BorderLayout.WEST, newParserPanel); + parserPanel = newParserPanel; + } + } + if (returnFocusToSearchField) + { + focusOnDefaultComponent(); + } + } // }}} - //{{{ Inner classes + // {{{ removeParserPanel() method + void removeParserPanel() + { + if (parserPanel != null) + { + toolBox.remove(parserPanel); + parserPanel = null; + } + } // }}} - //{{{ CustomTree class - /** - * A JTree with added mouse handling. Other plugins providing similar trees - * can extend CustomTree and override the mouse methods. - */ - protected class CustomTree extends JTree - { - protected CustomTree(TreeModel model) - { - super(model); - } + // {{{ expandTreeWithDelay() method + /** + * Expands the tree after a delay. + * The delay timer is restarted each time this method is called. + */ + protected void expandTreeWithDelay() + { + if (caretTimer != null) + { + caretTimer.stop(); + } + else + { + caretTimer = new Timer(0, new ActionListener() + { + public void actionPerformed(ActionEvent evt) + { + // If the filter is *not* persistent, then clear + // it when the tree is expanded for the current + // caret position. + if (!jEdit.getBooleanProperty("sidekick.persistentFilter")) + { + if (searchField.getText().length() > 0) + { + searchField.setText(""); + updateFilter(false); + } + } + TextArea textArea = view.getTextArea(); + int caret = textArea.getCaretPosition(); + Selection s = textArea.getSelectionAtOffset(caret); + expandTreeAt(s == null ? caret : s.getStart()); + } + } ); + caretTimer.setInitialDelay(500); + caretTimer.setRepeats(false); + } + caretTimer.start(); + } // }}} - protected void processMouseEvent(MouseEvent evt) - { - switch(evt.getID()) - { - //{{{ MOUSE_PRESSED... - case MouseEvent.MOUSE_PRESSED: - TreePath path = getPathForLocation( - evt.getX(),evt.getY()); - if(path != null) - { - Object value = ((DefaultMutableTreeNode)path - .getLastPathComponent()).getUserObject(); + // {{{ expandTreeAt() method + protected void expandTreeAt(int dot) + { + if (data == null) + { + return; + } - if(value instanceof IAsset) - { - IAsset asset = (IAsset)value; + TreePath treePath = data.getTreePathForPosition(dot); + if (treePath != null) + { + tree.expandPath(treePath); + tree.setSelectionPath(treePath); + if (jEdit.getBooleanProperty("sidekick.scrollToVisible")) + { + Rectangle r = tree.getPathBounds(treePath); + if (r != null) + { + r.width = 1; + tree.scrollRectToVisible(r); + } + else + { + tree.scrollPathToVisible(treePath); + } + } + } + } // }}} - JEditTextArea textArea = view.getTextArea(); - EditPane editPane = view.getEditPane(); + protected void expandCurrentNode() + { + DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); + TreePath path = new TreePath(node.getPath()); + tree.expandPath(path); + } - if(evt.getClickCount() == 2) - { - doubleClicked(view,asset,path); - } - else if(evt.isShiftDown()) - { - shiftClick(view,asset,path); - } - else if(evt.isControlDown()) - { - controlClick(view,asset,path); - } - else { - EditBus.send(new PositionChanging(editPane)); - textArea.setCaretPosition(asset.getStart().getOffset()); - } + protected void collapseCurrentNode() + { + DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); + TreePath path = new TreePath(node.getPath()); + tree.collapsePath(path); + } - } - if (!jEdit.getBooleanProperty("sidekick.persistentFilter")) { - if (searchField.getText().length() > 0) { - searchField.setText(""); - updateFilter(false); - } - } - } - super.processMouseEvent(evt); - searchField.requestFocusInWindow(); - if (path != null) { - selectPath(path); - } - break; //}}} - //{{{ MOUSE_EXITED... - case MouseEvent.MOUSE_EXITED: - view.getStatus().setMessage(null); - super.processMouseEvent(evt); - break; //}}} - default: - super.processMouseEvent(evt); - break; - } - } + // {{{ Inner classes - protected void doubleClicked(View view, IAsset asset, TreePath path) + // {{{ CustomTree class + /** + * A JTree with added mouse handling. Other plugins providing similar trees + * can extend CustomTree and override the mouse methods. + */ + protected class CustomTree extends JTree + { + protected CustomTree(TreeModel model) + { + super(model); + } + + public Enumeration<TreePath> getExpandedDescendants(TreePath parent) { + return null; } + + protected void processMouseEvent(MouseEvent evt) + { + switch (evt.getID() ) + { + // {{{ MOUSE_PRESSED... + case MouseEvent.MOUSE_PRESSED: + TreePath path = getPathForLocation(evt.getX(), evt.getY()); + if (path != null) + { + Object value = ((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject(); - protected void shiftClick(View view, IAsset asset, TreePath path) - { - JEditTextArea textArea = view.getTextArea(); - textArea.setCaretPosition(asset.getEnd().getOffset()); - Selection.Range range = new Selection.Range( - asset.getStart().getOffset(), asset.getEnd().getOffset()); - textArea.addToSelection(range); - } + if (value instanceof IAsset) + { + IAsset asset = (IAsset) value; - protected void controlClick(View view, IAsset asset, TreePath path) - { - JEditTextArea textArea = view.getTextArea(); - textArea.getDisplayManager().narrow( - textArea.getLineOfOffset(asset.getStart().getOffset()), - textArea.getLineOfOffset(asset.getEnd().getOffset())); - } - } //}}} + JEditTextArea textArea = view.getTextArea(); + EditPane editPane = view.getEditPane(); - //{{{ ActionHandler class - class ActionHandler implements ActionListener - { - /** A counter for counting how deep in recursion we are. - * Since a call to reloadParserCombo can cause itemselected events - * from the parserCombo, - */ - int level=0; - public void actionPerformed(ActionEvent evt) - { + if (evt.getClickCount() == 2) + { + doubleClicked(view, asset, path); + } + else if (evt.isShiftDown()) + { + shiftClick(view, asset, path); + } + else if (evt.isControlDown()) + { + controlClick(view, asset, path); + } + else + { + EditBus.send(new PositionChanging(editPane)); + textArea.setCaretPosition(asset.getStart().getOffset()); + } - // Workaround to avoid infinite recursion as a result of parsercombos - // updating - synchronized (this) { - if (evt.getSource() == parseBtn) level=0; - level++; - if (level>1) { - level--; - return; - } - } - Buffer b = view.getBuffer(); - jEdit.setIntegerProperty("sidekick.splitter.location", splitter.getDividerLocation()); - if (evt.getSource() == onSave) { - SideKick.setParseOnSave(onSave.isSelected()); - propertiesChanged(); - } - if (evt.getSource() == followCaret) { - boolean v = followCaret.isSelected(); - SideKick.setFollowCaret(followCaret.isSelected()); - if (v) { - onChange.setSelected(true); - } - propertiesChanged(); - } + } + if (!jEdit.getBooleanProperty("sidekick.persistentFilter")) + { + if (searchField.getText().length() > 0) + { + searchField.setText(""); + updateFilter(false); + } + } + } + super.processMouseEvent(evt); + searchField.requestFocusInWindow(); + if (path != null) + { + selectPath(path); + } + break; // }}} + // {{{ MOUSE_EXITED... + case MouseEvent.MOUSE_EXITED: + view.getStatus().setMessage(null); + super.processMouseEvent(evt); + break; // }}} + default: + super.processMouseEvent(evt); + break; + } + } - else if (evt.getSource() == onChange) { - boolean v = onChange.isSelected(); - SideKick.setParseOnChange(v); - if (!v) followCaret.setSelected(false); - propertiesChanged(); - } - else if (evt.getSource() == parserCombo ) { - Object selectedParser = parserCombo.getSelectedItem(); -// String preferredParser = b.getStringProperty(SideKickPlugin.PARSER_PROPERTY); - if (selectedParser.toString().equals(SideKickPlugin.NONE)) { - b.setProperty("usermode", Boolean.TRUE); - SideKickPlugin.setParserForBuffer(b, selectedParser.toString()); - } - els... [truncated message content] |