mirror of
https://github.com/processing/processing4.git
synced 2026-02-02 21:29:17 +01:00
Remove unused Sketch and Tab outlines
This commit is contained in:
@@ -1,388 +0,0 @@
|
||||
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||
|
||||
/*
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
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.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package processing.mode.java.pdex;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Point;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.event.WindowFocusListener;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.JTree;
|
||||
import javax.swing.ScrollPaneConstants;
|
||||
import javax.swing.SwingWorker;
|
||||
import javax.swing.event.DocumentEvent;
|
||||
import javax.swing.event.DocumentListener;
|
||||
import javax.swing.event.TreeSelectionEvent;
|
||||
import javax.swing.event.TreeSelectionListener;
|
||||
import javax.swing.tree.DefaultMutableTreeNode;
|
||||
import javax.swing.tree.DefaultTreeCellRenderer;
|
||||
import javax.swing.tree.DefaultTreeModel;
|
||||
import javax.swing.tree.TreeSelectionModel;
|
||||
|
||||
import org.eclipse.jdt.core.dom.ASTNode;
|
||||
import org.eclipse.jdt.core.dom.FieldDeclaration;
|
||||
import org.eclipse.jdt.core.dom.MethodDeclaration;
|
||||
import org.eclipse.jdt.core.dom.TypeDeclaration;
|
||||
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
|
||||
|
||||
import processing.app.Mode;
|
||||
import processing.mode.java.JavaEditor;
|
||||
|
||||
|
||||
public class SketchOutline {
|
||||
protected final JavaEditor editor;
|
||||
|
||||
protected JFrame frmOutlineView;
|
||||
protected JScrollPane jsp;
|
||||
protected DefaultMutableTreeNode soNode, tempNode;
|
||||
protected final JTree soTree;
|
||||
protected JTextField searchField;
|
||||
protected boolean internalSelection = false;
|
||||
|
||||
ImageIcon classIcon, fieldIcon, methodIcon;
|
||||
|
||||
|
||||
public SketchOutline(JavaEditor editor, DefaultMutableTreeNode codeTree) {
|
||||
this.editor = editor;
|
||||
soNode = new DefaultMutableTreeNode();
|
||||
generateSketchOutlineTree(soNode, codeTree);
|
||||
soNode = (DefaultMutableTreeNode) soNode.getChildAt(0);
|
||||
tempNode = soNode;
|
||||
soTree = new JTree(soNode);
|
||||
|
||||
Mode mode = editor.getMode();
|
||||
classIcon = mode.loadIcon("theme/icon_class_obj.png");
|
||||
methodIcon = mode.loadIcon("theme/icon_methpub_obj.png");
|
||||
fieldIcon = mode.loadIcon("theme/icon_field_protected_obj.png");
|
||||
|
||||
createGUI();
|
||||
}
|
||||
|
||||
|
||||
private void createGUI(){
|
||||
frmOutlineView = new JFrame();
|
||||
frmOutlineView.setAlwaysOnTop(true);
|
||||
frmOutlineView.setUndecorated(true);
|
||||
Point tp = editor.getTextArea().getLocationOnScreen();
|
||||
|
||||
int minWidth = (int) (editor.getMinimumSize().width * 0.7f);
|
||||
int maxWidth = (int) (editor.getMinimumSize().width * 0.9f);
|
||||
frmOutlineView.setLayout(new BoxLayout(frmOutlineView.getContentPane(),
|
||||
BoxLayout.Y_AXIS));
|
||||
JPanel panelTop = new JPanel(), panelBottom = new JPanel();
|
||||
panelTop.setLayout(new BoxLayout(panelTop, BoxLayout.Y_AXIS));
|
||||
panelBottom.setLayout(new BoxLayout(panelBottom, BoxLayout.Y_AXIS));
|
||||
searchField = new JTextField();
|
||||
searchField.setMinimumSize(new Dimension(minWidth, 25));
|
||||
panelTop.add(searchField);
|
||||
|
||||
jsp = new JScrollPane();
|
||||
|
||||
soTree.getSelectionModel()
|
||||
.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
||||
soTree.setRootVisible(false);
|
||||
soTree.setCellRenderer(new CustomCellRenderer());
|
||||
for (int i = 0; i < soTree.getRowCount(); i++) {
|
||||
soTree.expandRow(i);
|
||||
}
|
||||
soTree.setSelectionRow(0);
|
||||
|
||||
jsp.setViewportView(soTree);
|
||||
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
|
||||
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||||
jsp.setMinimumSize(new Dimension(minWidth, editor.getTextArea().getHeight() - 10));
|
||||
jsp.setMaximumSize(new Dimension(maxWidth, editor.getTextArea().getHeight() - 10));
|
||||
|
||||
panelBottom.add(jsp);
|
||||
frmOutlineView.add(panelTop);
|
||||
frmOutlineView.add(panelBottom);
|
||||
frmOutlineView.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
frmOutlineView.pack();
|
||||
frmOutlineView.setBounds(tp.x + editor.getTextArea().getWidth() - minWidth, tp.y, minWidth,
|
||||
Math.min(editor.getTextArea().getHeight(), frmOutlineView.getHeight()));
|
||||
frmOutlineView.setMinimumSize(new Dimension(minWidth, Math.min(editor.getTextArea().getHeight(), frmOutlineView.getHeight())));
|
||||
frmOutlineView.setLocation(tp.x + editor.getTextArea().getWidth()/2 - frmOutlineView.getWidth()/2,
|
||||
frmOutlineView.getY() + (editor.getTextArea().getHeight() - frmOutlineView.getHeight()) / 2);
|
||||
addListeners();
|
||||
}
|
||||
|
||||
|
||||
protected void addListeners() {
|
||||
|
||||
searchField.addKeyListener(new KeyAdapter() {
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
if (soTree.getRowCount() == 0)
|
||||
return;
|
||||
|
||||
internalSelection = true;
|
||||
|
||||
if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
||||
close();
|
||||
|
||||
} else if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||
if (soTree.getLastSelectedPathComponent() != null) {
|
||||
DefaultMutableTreeNode tnode = (DefaultMutableTreeNode) soTree
|
||||
.getLastSelectedPathComponent();
|
||||
if (tnode.getUserObject() instanceof ASTNode) {
|
||||
ASTNode awrap = (ASTNode) tnode.getUserObject();
|
||||
// TODO: highlight ASTNode
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
} else if (evt.getKeyCode() == KeyEvent.VK_UP) {
|
||||
if (soTree.getLastSelectedPathComponent() == null) {
|
||||
soTree.setSelectionRow(0);
|
||||
return;
|
||||
}
|
||||
|
||||
int x = soTree.getLeadSelectionRow() - 1;
|
||||
int step = jsp.getVerticalScrollBar().getMaximum()
|
||||
/ soTree.getRowCount();
|
||||
if (x == -1) {
|
||||
x = soTree.getRowCount() - 1;
|
||||
jsp.getVerticalScrollBar().setValue(jsp.getVerticalScrollBar().getMaximum());
|
||||
} else {
|
||||
jsp.getVerticalScrollBar().setValue((jsp.getVerticalScrollBar()
|
||||
.getValue() - step));
|
||||
}
|
||||
soTree.setSelectionRow(x);
|
||||
|
||||
} else if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
|
||||
if (soTree.getLastSelectedPathComponent() == null) {
|
||||
soTree.setSelectionRow(0);
|
||||
return;
|
||||
}
|
||||
int x = soTree.getLeadSelectionRow() + 1;
|
||||
|
||||
int step = jsp.getVerticalScrollBar().getMaximum()
|
||||
/ soTree.getRowCount();
|
||||
if (x == soTree.getRowCount()) {
|
||||
x = 0;
|
||||
jsp.getVerticalScrollBar().setValue(jsp.getVerticalScrollBar().getMinimum());
|
||||
} else {
|
||||
jsp.getVerticalScrollBar().setValue((jsp.getVerticalScrollBar()
|
||||
.getValue() + step));
|
||||
}
|
||||
soTree.setSelectionRow(x);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
searchField.getDocument().addDocumentListener(new DocumentListener() {
|
||||
|
||||
public void insertUpdate(DocumentEvent e) {
|
||||
updateSelection();
|
||||
}
|
||||
|
||||
public void removeUpdate(DocumentEvent e) {
|
||||
updateSelection();
|
||||
}
|
||||
|
||||
public void changedUpdate(DocumentEvent e) {
|
||||
updateSelection();
|
||||
}
|
||||
|
||||
private void updateSelection(){
|
||||
SwingWorker<Object, Object> worker = new SwingWorker<Object, Object>() {
|
||||
protected Object doInBackground() throws Exception {
|
||||
String text = searchField.getText().toLowerCase();
|
||||
tempNode = new DefaultMutableTreeNode();
|
||||
filterTree(text, tempNode, soNode); // TODO: is using soNode thread-safe? [jv]
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void done() {
|
||||
soTree.setModel(new DefaultTreeModel(tempNode));
|
||||
((DefaultTreeModel) soTree.getModel()).reload();
|
||||
for (int i = 0; i < soTree.getRowCount(); i++) {
|
||||
soTree.expandRow(i);
|
||||
}
|
||||
internalSelection = true;
|
||||
soTree.setSelectionRow(0);
|
||||
}
|
||||
};
|
||||
worker.execute();
|
||||
}
|
||||
});
|
||||
|
||||
frmOutlineView.addWindowFocusListener(new WindowFocusListener() {
|
||||
public void windowLostFocus(WindowEvent e) {
|
||||
close();
|
||||
}
|
||||
|
||||
public void windowGainedFocus(WindowEvent e) {
|
||||
}
|
||||
});
|
||||
|
||||
soTree.addTreeSelectionListener(new TreeSelectionListener() {
|
||||
|
||||
public void valueChanged(TreeSelectionEvent e) {
|
||||
|
||||
if (internalSelection) {
|
||||
internalSelection = (false);
|
||||
return;
|
||||
}
|
||||
// log(e);
|
||||
scrollToNode();
|
||||
}
|
||||
});
|
||||
|
||||
soTree.addMouseListener(new MouseAdapter() {
|
||||
public void mouseClicked(MouseEvent me) {
|
||||
scrollToNode();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void scrollToNode() {
|
||||
if (soTree.getLastSelectedPathComponent() == null) {
|
||||
return;
|
||||
}
|
||||
DefaultMutableTreeNode tnode = (DefaultMutableTreeNode) soTree
|
||||
.getLastSelectedPathComponent();
|
||||
if (tnode.getUserObject() instanceof ASTNode) {
|
||||
ASTNode awrap = (ASTNode) tnode.getUserObject();
|
||||
// TODO: highlight ASTNode
|
||||
close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected boolean filterTree(String prefix, DefaultMutableTreeNode tree,
|
||||
DefaultMutableTreeNode mainTree) {
|
||||
if (mainTree.isLeaf()) {
|
||||
return mainTree.getUserObject().toString().toLowerCase().startsWith(prefix);
|
||||
}
|
||||
|
||||
boolean found = false;
|
||||
for (int i = 0; i < mainTree.getChildCount(); i++) {
|
||||
DefaultMutableTreeNode tNode = new DefaultMutableTreeNode(
|
||||
((DefaultMutableTreeNode) mainTree
|
||||
.getChildAt(i))
|
||||
.getUserObject());
|
||||
if (filterTree(prefix, tNode,
|
||||
(DefaultMutableTreeNode) mainTree.getChildAt(i))) {
|
||||
found = true;
|
||||
tree.add(tNode);
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void generateSketchOutlineTree(DefaultMutableTreeNode node,
|
||||
DefaultMutableTreeNode codetree) {
|
||||
if (codetree == null)
|
||||
return;
|
||||
//log("Visi " + codetree + codetree.getUserObject().getClass().getSimpleName());
|
||||
if (!(codetree.getUserObject() instanceof ASTNode))
|
||||
return;
|
||||
ASTNode awnode = (ASTNode) codetree.getUserObject(), aw2 = null;
|
||||
|
||||
if (awnode instanceof TypeDeclaration) {
|
||||
aw2 = ((TypeDeclaration) awnode).getName();
|
||||
} else if (awnode instanceof MethodDeclaration) {
|
||||
aw2 = ((MethodDeclaration) awnode).getName();
|
||||
} else if (awnode instanceof FieldDeclaration) {
|
||||
FieldDeclaration fd = (FieldDeclaration) awnode;
|
||||
for (VariableDeclarationFragment vdf : (List<VariableDeclarationFragment>) fd.fragments()) {
|
||||
DefaultMutableTreeNode newNode =
|
||||
new DefaultMutableTreeNode(vdf.getName());
|
||||
node.add(newNode);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (aw2 == null)
|
||||
return;
|
||||
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(aw2);
|
||||
node.add(newNode);
|
||||
for (int i = 0; i < codetree.getChildCount(); i++) {
|
||||
generateSketchOutlineTree(newNode,
|
||||
(DefaultMutableTreeNode) codetree.getChildAt(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void show() {
|
||||
frmOutlineView.setVisible(true);
|
||||
}
|
||||
|
||||
|
||||
public void close(){
|
||||
frmOutlineView.setVisible(false);
|
||||
frmOutlineView.dispose();
|
||||
}
|
||||
|
||||
|
||||
public boolean isVisible(){
|
||||
return frmOutlineView.isVisible();
|
||||
}
|
||||
|
||||
|
||||
protected class CustomCellRenderer extends DefaultTreeCellRenderer {
|
||||
|
||||
public Component getTreeCellRendererComponent(JTree tree, Object value,
|
||||
boolean sel, boolean expanded,
|
||||
boolean leaf, int row,
|
||||
boolean hasFocus) {
|
||||
super.getTreeCellRendererComponent(tree, value, sel, expanded,
|
||||
leaf, row, hasFocus);
|
||||
if (value instanceof DefaultMutableTreeNode)
|
||||
setIcon(getTreeIcon(value));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Icon getTreeIcon(Object o) {
|
||||
if (((DefaultMutableTreeNode) o).getUserObject() instanceof ASTNode) {
|
||||
ASTNode awrap = (ASTNode)
|
||||
((DefaultMutableTreeNode) o).getUserObject();
|
||||
|
||||
int type = awrap.getParent().getNodeType();
|
||||
if (type == ASTNode.METHOD_DECLARATION) {
|
||||
return methodIcon;
|
||||
} else if (type == ASTNode.TYPE_DECLARATION) {
|
||||
return classIcon;
|
||||
} else if (type == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
|
||||
return fieldIcon;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,337 +0,0 @@
|
||||
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||
|
||||
/*
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
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.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package processing.mode.java.pdex;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Point;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.awt.event.WindowFocusListener;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.JTree;
|
||||
import javax.swing.ScrollPaneConstants;
|
||||
import javax.swing.SwingWorker;
|
||||
import javax.swing.event.DocumentEvent;
|
||||
import javax.swing.event.DocumentListener;
|
||||
import javax.swing.event.TreeSelectionEvent;
|
||||
import javax.swing.event.TreeSelectionListener;
|
||||
import javax.swing.tree.DefaultMutableTreeNode;
|
||||
import javax.swing.tree.DefaultTreeCellRenderer;
|
||||
import javax.swing.tree.DefaultTreeModel;
|
||||
import javax.swing.tree.TreeSelectionModel;
|
||||
|
||||
import processing.app.SketchCode;
|
||||
import processing.mode.java.JavaEditor;
|
||||
|
||||
|
||||
public class TabOutline {
|
||||
protected JFrame frmOutlineView;
|
||||
protected JScrollPane jsp;
|
||||
protected DefaultMutableTreeNode tabNode;
|
||||
protected DefaultMutableTreeNode tempNode;
|
||||
protected JTree tabTree;
|
||||
protected JTextField searchField;
|
||||
protected JLabel lblCaption;
|
||||
protected JavaEditor editor;
|
||||
protected boolean internalSelection = false;
|
||||
|
||||
|
||||
public TabOutline(JavaEditor editor) {
|
||||
this.editor = editor;
|
||||
createGUI();
|
||||
}
|
||||
|
||||
|
||||
private void createGUI() {
|
||||
frmOutlineView = new JFrame();
|
||||
frmOutlineView.setAlwaysOnTop(true);
|
||||
frmOutlineView.setUndecorated(true);
|
||||
Point tp = editor.getTextArea().getLocationOnScreen();
|
||||
lblCaption = new JLabel("Tabs List (type to filter)");
|
||||
int minWidth = estimateFrameWidth();
|
||||
int maxWidth = (int) (editor.getMinimumSize().width * 0.9f);
|
||||
frmOutlineView.setLayout(new BoxLayout(frmOutlineView.getContentPane(),
|
||||
BoxLayout.Y_AXIS));
|
||||
JPanel panelTop = new JPanel(), panelMiddle = new JPanel(), panelBottom = new JPanel();
|
||||
panelTop.setLayout(new GridBagLayout());
|
||||
panelMiddle.setLayout(new BoxLayout(panelMiddle, BoxLayout.Y_AXIS));
|
||||
panelBottom.setLayout(new BoxLayout(panelBottom, BoxLayout.Y_AXIS));
|
||||
lblCaption.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||
panelTop.add(lblCaption);
|
||||
searchField = new JTextField();
|
||||
searchField.setMinimumSize(new Dimension(minWidth, 25));
|
||||
panelMiddle.add(searchField);
|
||||
|
||||
jsp = new JScrollPane();
|
||||
populateTabTree();
|
||||
jsp.setViewportView(tabTree);
|
||||
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
|
||||
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||||
jsp.setMinimumSize(new Dimension(minWidth, editor.getTextArea().getHeight() - 10));
|
||||
jsp.setMaximumSize(new Dimension(maxWidth, editor.getTextArea().getHeight() - 10));
|
||||
|
||||
panelBottom.add(jsp);
|
||||
frmOutlineView.add(panelTop);
|
||||
frmOutlineView.add(panelMiddle);
|
||||
frmOutlineView.add(panelBottom);
|
||||
frmOutlineView.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
frmOutlineView.pack();
|
||||
frmOutlineView.setBounds(tp.x + editor.getTextArea().getWidth() - minWidth,
|
||||
tp.y,
|
||||
minWidth,
|
||||
estimateFrameHeight());
|
||||
frmOutlineView.setMinimumSize(new Dimension(minWidth, Math
|
||||
.min(editor.getTextArea().getHeight(),
|
||||
frmOutlineView.getHeight())));
|
||||
frmOutlineView.setLocation(tp.x + editor.getTextArea().getWidth()/2 - frmOutlineView.getWidth()/2,
|
||||
frmOutlineView.getY() + (editor.getTextArea().getHeight() - frmOutlineView.getHeight()) / 2);
|
||||
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tabTree.getCellRenderer();
|
||||
renderer.setLeafIcon(null);
|
||||
renderer.setClosedIcon(null);
|
||||
renderer.setOpenIcon(null);
|
||||
addListeners();
|
||||
}
|
||||
|
||||
|
||||
private void addListeners() {
|
||||
searchField.addKeyListener(new KeyAdapter() {
|
||||
public void keyPressed(KeyEvent evt) {
|
||||
if (tabTree.getRowCount() == 0)
|
||||
return;
|
||||
|
||||
internalSelection = true;
|
||||
|
||||
if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
||||
close();
|
||||
} else if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||
if (tabTree.getLastSelectedPathComponent() != null) {
|
||||
DefaultMutableTreeNode tnode = (DefaultMutableTreeNode) tabTree
|
||||
.getLastSelectedPathComponent();
|
||||
//log("Enter Key, Tab: " + tnode);
|
||||
switchToTab(tnode.toString());
|
||||
close();
|
||||
}
|
||||
} else if (evt.getKeyCode() == KeyEvent.VK_UP) {
|
||||
if (tabTree.getLastSelectedPathComponent() == null) {
|
||||
tabTree.setSelectionRow(0);
|
||||
return;
|
||||
}
|
||||
|
||||
int x = tabTree.getLeadSelectionRow() - 1;
|
||||
int step = jsp.getVerticalScrollBar().getMaximum()
|
||||
/ tabTree.getRowCount();
|
||||
if (x == -1) {
|
||||
x = tabTree.getRowCount() - 1;
|
||||
jsp.getVerticalScrollBar().setValue(jsp.getVerticalScrollBar()
|
||||
.getMaximum());
|
||||
} else {
|
||||
jsp.getVerticalScrollBar().setValue((jsp.getVerticalScrollBar()
|
||||
.getValue() - step));
|
||||
}
|
||||
tabTree.setSelectionRow(x);
|
||||
} else if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
|
||||
if (tabTree.getLastSelectedPathComponent() == null) {
|
||||
tabTree.setSelectionRow(0);
|
||||
return;
|
||||
}
|
||||
int x = tabTree.getLeadSelectionRow() + 1;
|
||||
|
||||
int step = jsp.getVerticalScrollBar().getMaximum()
|
||||
/ tabTree.getRowCount();
|
||||
if (x == tabTree.getRowCount()) {
|
||||
x = 0;
|
||||
jsp.getVerticalScrollBar().setValue(jsp.getVerticalScrollBar()
|
||||
.getMinimum());
|
||||
} else {
|
||||
jsp.getVerticalScrollBar().setValue((jsp.getVerticalScrollBar()
|
||||
.getValue() + step));
|
||||
}
|
||||
tabTree.setSelectionRow(x);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
searchField.getDocument().addDocumentListener(new DocumentListener() {
|
||||
|
||||
public void insertUpdate(DocumentEvent e) {
|
||||
updateSelection();
|
||||
}
|
||||
|
||||
public void removeUpdate(DocumentEvent e) {
|
||||
updateSelection();
|
||||
}
|
||||
|
||||
public void changedUpdate(DocumentEvent e) {
|
||||
updateSelection();
|
||||
}
|
||||
|
||||
private void updateSelection() {
|
||||
SwingWorker<Object, Object> worker = new SwingWorker<Object, Object>() {
|
||||
protected Object doInBackground() throws Exception {
|
||||
String text = searchField.getText().toLowerCase();
|
||||
tempNode = new DefaultMutableTreeNode();
|
||||
filterTree(text, tempNode, tabNode); // TODO: is using tabNode thread-safe? [jv]
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void done() {
|
||||
tabTree.setModel(new DefaultTreeModel(tempNode));
|
||||
((DefaultTreeModel) tabTree.getModel()).reload();
|
||||
// for (int i = 0; i < tabTree.getRowCount(); i++) {
|
||||
// tabTree.expandRow(i);
|
||||
// }
|
||||
internalSelection = true;
|
||||
tabTree.setSelectionRow(0);
|
||||
}
|
||||
};
|
||||
worker.execute();
|
||||
}
|
||||
});
|
||||
|
||||
tabTree.addTreeSelectionListener(new TreeSelectionListener() {
|
||||
|
||||
public void valueChanged(TreeSelectionEvent e) {
|
||||
if (internalSelection) {
|
||||
//log("Internal selection");
|
||||
internalSelection = (false);
|
||||
return;
|
||||
}
|
||||
// log(e);
|
||||
|
||||
if (tabTree.getLastSelectedPathComponent() == null) {
|
||||
return;
|
||||
}
|
||||
DefaultMutableTreeNode tnode = (DefaultMutableTreeNode) tabTree
|
||||
.getLastSelectedPathComponent();
|
||||
//log("Clicked " + tnode);
|
||||
switchToTab(tnode.toString());
|
||||
close();
|
||||
}
|
||||
});
|
||||
|
||||
tabTree.addMouseListener(new MouseAdapter() {
|
||||
public void mouseClicked(MouseEvent me) {
|
||||
if (tabTree.getLastSelectedPathComponent() == null) {
|
||||
return;
|
||||
}
|
||||
DefaultMutableTreeNode tnode =
|
||||
(DefaultMutableTreeNode) tabTree.getLastSelectedPathComponent();
|
||||
//log("Clicked " + tnode);
|
||||
switchToTab(tnode.toString());
|
||||
close();
|
||||
}
|
||||
});
|
||||
|
||||
frmOutlineView.addWindowFocusListener(new WindowFocusListener() {
|
||||
public void windowLostFocus(WindowEvent e) {
|
||||
close();
|
||||
}
|
||||
|
||||
public void windowGainedFocus(WindowEvent e) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void switchToTab(String tabName) {
|
||||
for (SketchCode sc : editor.getSketch().getCode()) {
|
||||
if (sc.getPrettyName().equals(tabName)) {
|
||||
editor.getSketch().setCurrentCode(editor.getSketch().getCodeIndex(sc));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void populateTabTree() {
|
||||
tabNode = new DefaultMutableTreeNode("Tabs");
|
||||
for (SketchCode sc : editor.getSketch().getCode()) {
|
||||
DefaultMutableTreeNode tab = new DefaultMutableTreeNode(
|
||||
sc.getPrettyName());
|
||||
tabNode.add(tab);
|
||||
}
|
||||
tempNode = tabNode;
|
||||
tabTree = new JTree(tabNode);
|
||||
tabTree.getSelectionModel()
|
||||
.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
||||
tabTree.setRootVisible(false);
|
||||
tabTree.setSelectionRow(editor.getSketch().getCurrentCodeIndex());
|
||||
}
|
||||
|
||||
protected boolean filterTree(String prefix, DefaultMutableTreeNode tree,
|
||||
DefaultMutableTreeNode mainTree) {
|
||||
if (mainTree.isLeaf()) {
|
||||
return (mainTree.getUserObject().toString().toLowerCase()
|
||||
.startsWith(prefix));
|
||||
}
|
||||
|
||||
boolean found = false;
|
||||
for (int i = 0; i < mainTree.getChildCount(); i++) {
|
||||
DefaultMutableTreeNode tNode = new DefaultMutableTreeNode(
|
||||
((DefaultMutableTreeNode) mainTree
|
||||
.getChildAt(i))
|
||||
.getUserObject());
|
||||
if (filterTree(prefix, tNode,
|
||||
(DefaultMutableTreeNode) mainTree.getChildAt(i))) {
|
||||
found = true;
|
||||
tree.add(tNode);
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
private int estimateFrameWidth() {
|
||||
FontMetrics fm = editor.getTextArea().getGraphics().getFontMetrics();
|
||||
int w = fm.stringWidth(lblCaption.getText()) + 10;
|
||||
for (int i = 0; i < editor.getSketch().getCodeCount(); i++) {
|
||||
w = Math.max(w, fm.stringWidth(editor.getSketch().getCode(i).getPrettyName()) + 10);
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
private int estimateFrameHeight() {
|
||||
int textHeight = jsp.getGraphics().getFontMetrics().getHeight() + 2;
|
||||
int t = Math.max(4, editor.getSketch().getCodeCount() + 3);
|
||||
return Math.min(textHeight * t, frmOutlineView.getHeight());
|
||||
}
|
||||
|
||||
public void show() {
|
||||
frmOutlineView.setVisible(true);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
frmOutlineView.setVisible(false);
|
||||
frmOutlineView.dispose();
|
||||
}
|
||||
|
||||
public boolean isVisible() {
|
||||
return frmOutlineView.isVisible();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user