Revision 44263 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultJExpressionBuilder.java
DefaultJExpressionBuilder.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.awt.BorderLayout; |
4 | 4 |
import java.awt.Dimension; |
5 |
import java.awt.Point; |
|
5 | 6 |
import java.awt.Toolkit; |
6 | 7 |
import java.awt.datatransfer.Clipboard; |
7 | 8 |
import java.awt.datatransfer.StringSelection; |
... | ... | |
47 | 48 |
import org.apache.commons.io.FilenameUtils; |
48 | 49 |
import org.apache.commons.io.IOUtils; |
49 | 50 |
import org.apache.commons.lang3.StringUtils; |
51 |
import org.gvsig.expressionevaluator.Bookmark; |
|
52 |
import org.gvsig.expressionevaluator.Bookmarks; |
|
50 | 53 |
import org.gvsig.expressionevaluator.Expression; |
51 | 54 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
55 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
|
56 |
import org.gvsig.expressionevaluator.History; |
|
52 | 57 |
import org.gvsig.expressionevaluator.SymbolTable; |
53 | 58 |
import org.gvsig.expressionevaluator.swing.Element; |
54 | 59 |
import org.gvsig.expressionevaluator.swing.Element.GroupElement; |
... | ... | |
65 | 70 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
66 | 71 |
import org.gvsig.tools.swing.api.script.ScriptSwingManager; |
67 | 72 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
73 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
|
74 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
|
75 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
|
68 | 76 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
69 | 77 |
|
70 | 78 |
/** |
... | ... | |
77 | 85 |
implements JExpressionBuilder { |
78 | 86 |
|
79 | 87 |
private final ExpressionEvaluatorSwingManager manager; |
80 |
private GroupElement currentGroup;
|
|
88 |
private Element currentElement;
|
|
81 | 89 |
private PickerController<List<Script>> scriptPicker; |
82 | 90 |
private ScriptSwingManager.ScriptEditor scriptEditor; |
83 | 91 |
|
... | ... | |
200 | 208 |
|
201 | 209 |
private void reload(Element element) { |
202 | 210 |
if (element instanceof Element) { |
203 |
if (element instanceof GroupElement) { |
|
211 |
// if (element instanceof GroupElement) {
|
|
204 | 212 |
reload(element); |
205 |
} |
|
213 |
// }
|
|
206 | 214 |
element.reload(); |
207 | 215 |
} |
208 | 216 |
} |
... | ... | |
210 | 218 |
private List<Element> getChilds(final GroupElement node) { |
211 | 219 |
List<Element> x = new ArrayList<>(); |
212 | 220 |
for (Element element : node.getElements()) { |
213 |
if (element instanceof GroupElement) { |
|
221 |
// if (element instanceof GroupElement) {
|
|
214 | 222 |
x.add(element); |
215 |
} |
|
223 |
// }
|
|
216 | 224 |
} |
217 | 225 |
return x; //node.getElements(); |
218 | 226 |
} |
... | ... | |
232 | 240 |
|
233 | 241 |
@Override |
234 | 242 |
public boolean isLeaf(Object node) { |
235 |
if( node instanceof GroupElement ) { |
|
236 |
return !((GroupElement) node).hasSubgroups(); |
|
237 |
} |
|
238 |
return false; |
|
243 |
return !(node instanceof GroupElement); |
|
239 | 244 |
} |
240 | 245 |
|
241 | 246 |
@Override |
... | ... | |
302 | 307 |
) { |
303 | 308 |
@Override |
304 | 309 |
protected Expression getExpression() { |
305 |
return DefaultJExpressionBuilder.this.getExpression(); |
|
310 |
return DefaultJExpressionBuilder.this.getExpressionWithOutHistory();
|
|
306 | 311 |
} |
307 | 312 |
|
308 | 313 |
@Override |
... | ... | |
432 | 437 |
public void valueChanged(TreeSelectionEvent e) { |
433 | 438 |
TreePath path = e.getPath(); |
434 | 439 |
Element element = (Element) path.getLastPathComponent(); |
435 |
if (element instanceof GroupElement) { |
|
436 |
updateSimpleElementsList((GroupElement) element); |
|
437 |
} |
|
440 |
doSelectElement(element); |
|
441 |
updateSimpleElementsList(element); |
|
438 | 442 |
} |
439 | 443 |
}); |
440 | 444 |
this.treeElements.addMouseListener(new MouseAdapter() { |
... | ... | |
454 | 458 |
this.btnGroupElementInsert.addActionListener(new ActionListener() { |
455 | 459 |
@Override |
456 | 460 |
public void actionPerformed(ActionEvent e) { |
457 |
doInsert(txtGroupElement.getText()); |
|
461 |
if( currentElement!=null ) { |
|
462 |
doInsert(currentElement.getRenderedValue()); |
|
463 |
} else { |
|
464 |
doInsert(txtGroupElement.getText()); |
|
465 |
} |
|
458 | 466 |
} |
459 | 467 |
}); |
460 | 468 |
this.btnSimpleElementInsert.addActionListener(new ActionListener() { |
... | ... | |
620 | 628 |
|
621 | 629 |
this.tabExpressionBuilder.setEnabledAt(0, this.builderConfig.getPreferences().getScriptsEnabled()); |
622 | 630 |
|
631 |
this.btnHistory.addActionListener(new ActionListener() { |
|
632 |
@Override |
|
633 |
public void actionPerformed(ActionEvent e) { |
|
634 |
doHistory(); |
|
635 |
} |
|
636 |
}); |
|
637 |
|
|
638 |
this.btnBookmarks.addActionListener(new ActionListener() { |
|
639 |
@Override |
|
640 |
public void actionPerformed(ActionEvent e) { |
|
641 |
doBookmarks(); |
|
642 |
} |
|
643 |
}); |
|
644 |
|
|
645 |
|
|
623 | 646 |
this.infoPanel.repaint(); |
624 | 647 |
|
625 |
this.setPreferredSize(new Dimension(650, 350));
|
|
648 |
this.setPreferredSize(new Dimension(750, 450));
|
|
626 | 649 |
|
627 | 650 |
|
628 | 651 |
Dimension dim = this.getPreferredSize(); |
... | ... | |
650 | 673 |
return this.txtExpression.getText(); |
651 | 674 |
} |
652 | 675 |
|
676 |
@Override |
|
653 | 677 |
public void setText(String text) { |
654 | 678 |
this.txtExpression.setText(text); |
655 | 679 |
} |
... | ... | |
688 | 712 |
// } |
689 | 713 |
} |
690 | 714 |
|
691 |
private void updateSimpleElementsList(GroupElement group) {
|
|
715 |
private void updateSimpleElementsList(Element element) {
|
|
692 | 716 |
FilteredListModel model = new FilteredListModel(); |
693 | 717 |
model.addElement("Loading..."); |
694 |
this.currentGroup = group;
|
|
695 |
this.txtGroupElement.setText(group.getName());
|
|
718 |
this.currentElement = element;
|
|
719 |
this.txtGroupElement.setText(element.getName());
|
|
696 | 720 |
this.txtSimpleElementFilter.setText(""); |
697 |
this.infoPanel.clean(); |
|
698 |
this.infoPanel.repaint(); |
|
699 |
if (group instanceof SimpleElement) {
|
|
721 |
// this.infoPanel.clean();
|
|
722 |
// this.infoPanel.repaint();
|
|
723 |
if (element instanceof SimpleElement) {
|
|
700 | 724 |
this.btnGroupElementInsert.setEnabled(true); |
701 | 725 |
} else { |
702 | 726 |
this.btnGroupElementInsert.setEnabled(false); |
... | ... | |
714 | 738 |
} |
715 | 739 |
|
716 | 740 |
private void simpleElementsUpdater() { |
717 |
final GroupElement group = this.currentGroup;
|
|
741 |
final Element element = this.currentElement;
|
|
718 | 742 |
final FilteredListModel model = new FilteredListModel(); |
719 |
for (Element child : group) { |
|
720 |
if (group != this.currentGroup) { |
|
743 |
for (Element value : element.getValues()) { |
|
744 |
if (element != this.currentElement) { |
|
745 |
// Como estamos en otro hilo, cuando el usuario cambia de elemento, |
|
746 |
// cancelamos las actualizaciones que teniamos en curso. |
|
721 | 747 |
return; |
722 | 748 |
} |
723 |
if (child instanceof SimpleElement) {
|
|
724 |
model.addElement(child);
|
|
749 |
if (value instanceof SimpleElement) {
|
|
750 |
model.addElement(value);
|
|
725 | 751 |
} |
726 | 752 |
} |
727 | 753 |
model.sort(true); |
728 | 754 |
SwingUtilities.invokeLater(new Runnable() { |
729 | 755 |
@Override |
730 | 756 |
public void run() { |
731 |
if (group != currentGroup) {
|
|
757 |
if (element != currentElement) {
|
|
732 | 758 |
return; |
733 | 759 |
} |
734 |
if( group.hasMoreElements() ) {
|
|
760 |
if( element.hasMoreValues()) {
|
|
735 | 761 |
lblSimpleElementsMsg.setText("More elements..."); |
736 | 762 |
} else { |
737 | 763 |
lblSimpleElementsMsg.setText(""); |
... | ... | |
745 | 771 |
|
746 | 772 |
@Override |
747 | 773 |
public Expression getExpression() { |
774 |
Expression expression = this.getExpressionWithOutHistory(); |
|
775 |
ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager(); |
|
776 |
theManager.getHistory().add(expression); |
|
777 |
return expression; |
|
778 |
} |
|
779 |
|
|
780 |
@Override |
|
781 |
public Expression getExpressionWithOutHistory() { |
|
748 | 782 |
String phrase = this.txtExpression.getText(); |
749 | 783 |
if (StringUtils.isEmpty(phrase)) { |
750 | 784 |
return null; |
... | ... | |
901 | 935 |
} |
902 | 936 |
} |
903 | 937 |
} |
938 |
|
|
939 |
private void doHistory() { |
|
940 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
941 |
ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager(); |
|
942 |
JPopupMenu popup = new JPopupMenu(); |
|
943 |
History history = theManager.getHistory(); |
|
944 |
if( !history.isEmpty() ) { |
|
945 |
for (Expression expression : history) { |
|
946 |
popup.add(createHistoryMenuItem(expression)); |
|
947 |
} |
|
948 |
popup.addSeparator(); |
|
949 |
} |
|
950 |
JMenuItem showAllHistory = new JMenuItem(i18n.getTranslation("_Show_all_history")); |
|
951 |
showAllHistory.addActionListener(new ActionListener() { |
|
952 |
@Override |
|
953 |
public void actionPerformed(ActionEvent e) { |
|
954 |
doShowHistoryDialog(); |
|
955 |
} |
|
956 |
}); |
|
957 |
popup.add(showAllHistory); |
|
958 |
Point p = this.btnHistory.getLocationOnScreen(); |
|
959 |
popup.show(this.btnHistory, 0, this.btnHistory.getHeight()); |
|
960 |
} |
|
961 |
|
|
962 |
private JMenuItem createHistoryMenuItem(final Expression expression) { |
|
963 |
JMenuItem item = new JMenuItem(StringUtils.abbreviate(expression.getPhrase(), 45)); |
|
964 |
item.addActionListener(new ActionListener() { |
|
965 |
@Override |
|
966 |
public void actionPerformed(ActionEvent e) { |
|
967 |
setExpression(expression); |
|
968 |
} |
|
969 |
}); |
|
970 |
return item; |
|
971 |
} |
|
972 |
|
|
973 |
private void doShowHistoryDialog() { |
|
974 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
|
975 |
final DefaultHistoryPanel panel = new DefaultHistoryPanel(); |
|
976 |
final Dialog dialog = winManager.createDialog( |
|
977 |
panel, |
|
978 |
"History", |
|
979 |
null, |
|
980 |
WindowManager_v2.BUTTONS_OK_CANCEL |
|
981 |
); |
|
982 |
dialog.addActionListener(new ActionListener() { |
|
983 |
@Override |
|
984 |
public void actionPerformed(ActionEvent e) { |
|
985 |
if( dialog.getAction()!=WindowManager_v2.BUTTON_OK ) { |
|
986 |
return; |
|
987 |
} |
|
988 |
setExpression(panel.getExpression()); |
|
989 |
} |
|
990 |
}); |
|
991 |
dialog.show(WindowManager.MODE.DIALOG); |
|
992 |
} |
|
993 |
|
|
994 |
private void doBookmarks() { |
|
995 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
996 |
ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager(); |
|
997 |
JPopupMenu popup = new JPopupMenu(); |
|
998 |
JMenuItem addToBookmarks = new JMenuItem(i18n.getTranslation("_Add_to_bookmarks")); |
|
999 |
addToBookmarks.addActionListener(new ActionListener() { |
|
1000 |
@Override |
|
1001 |
public void actionPerformed(ActionEvent e) { |
|
1002 |
doShowBookmarksDialog(getExpressionWithOutHistory()); |
|
1003 |
} |
|
1004 |
}); |
|
1005 |
popup.add(addToBookmarks); |
|
1006 |
Bookmarks bookmarks = theManager.getBookmarks(); |
|
1007 |
if( !bookmarks.isEmpty() ) { |
|
1008 |
popup.addSeparator(); |
|
1009 |
for (Bookmark bookmark : bookmarks) { |
|
1010 |
popup.add(createBookmarkMenuItem(bookmark)); |
|
1011 |
} |
|
1012 |
popup.addSeparator(); |
|
1013 |
JMenuItem showAllBookmarks = new JMenuItem(i18n.getTranslation("_Show_all_bookmarks")); |
|
1014 |
showAllBookmarks.addActionListener(new ActionListener() { |
|
1015 |
@Override |
|
1016 |
public void actionPerformed(ActionEvent e) { |
|
1017 |
doShowBookmarksDialog(null); |
|
1018 |
} |
|
1019 |
}); |
|
1020 |
popup.add(showAllBookmarks); |
|
1021 |
} |
|
1022 |
Point p = this.btnBookmarks.getLocationOnScreen(); |
|
1023 |
popup.show(this.btnBookmarks, 0, this.btnBookmarks.getHeight()); |
|
1024 |
} |
|
1025 |
|
|
1026 |
private JMenuItem createBookmarkMenuItem(final Bookmark bookmark) { |
|
1027 |
JMenuItem item = new JMenuItem(StringUtils.abbreviate(bookmark.getName(), 45)); |
|
1028 |
item.addActionListener(new ActionListener() { |
|
1029 |
@Override |
|
1030 |
public void actionPerformed(ActionEvent e) { |
|
1031 |
setExpression(bookmark.getValue()); |
|
1032 |
} |
|
1033 |
}); |
|
1034 |
return item; |
|
1035 |
} |
|
1036 |
|
|
1037 |
private void doShowBookmarksDialog(Expression expression) { |
|
1038 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
|
1039 |
final DefaultBookmarksPanel panel = new DefaultBookmarksPanel(expression); |
|
1040 |
final Dialog dialog = winManager.createDialog( |
|
1041 |
panel, |
|
1042 |
"Bookmarks", |
|
1043 |
null, |
|
1044 |
WindowManager_v2.BUTTONS_OK_CANCEL |
|
1045 |
); |
|
1046 |
dialog.addActionListener(new ActionListener() { |
|
1047 |
@Override |
|
1048 |
public void actionPerformed(ActionEvent e) { |
|
1049 |
if( dialog.getAction()!=WindowManager_v2.BUTTON_OK ) { |
|
1050 |
return; |
|
1051 |
} |
|
1052 |
Bookmark bookmark = panel.getCurrentBookmark(); |
|
1053 |
if( bookmark!=null ) { |
|
1054 |
setExpression(bookmark.getValue()); |
|
1055 |
} |
|
1056 |
} |
|
1057 |
}); |
|
1058 |
dialog.show(WindowManager.MODE.DIALOG); |
|
1059 |
} |
|
904 | 1060 |
} |
Also available in: Unified diff