svn-gvsig-desktop / 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 / DefaultBookmarksPanel.java @ 44263
History | View | Annotate | Download (6.27 KB)
1 |
package org.gvsig.expressionevaluator.swing.impl; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.util.Date; |
7 |
import javax.swing.DefaultListModel; |
8 |
import javax.swing.ListModel; |
9 |
import javax.swing.event.ListSelectionEvent; |
10 |
import javax.swing.event.ListSelectionListener; |
11 |
import org.apache.commons.lang3.StringUtils; |
12 |
import org.gvsig.expressionevaluator.Bookmark; |
13 |
import org.gvsig.expressionevaluator.Bookmarks; |
14 |
import org.gvsig.expressionevaluator.Expression; |
15 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
16 |
import org.gvsig.tools.util.LabeledValue; |
17 |
|
18 |
/**
|
19 |
*
|
20 |
* @author jjdelcerro
|
21 |
*/
|
22 |
public class DefaultBookmarksPanel extends DefaultBookmarksPanelView { |
23 |
|
24 |
private static final int STATE_VIEWING = 0; |
25 |
private static final int STATE_MODIFYING = 1; |
26 |
|
27 |
private int state; |
28 |
private Bookmark currentBookmark;
|
29 |
private int currentIndex; |
30 |
|
31 |
public DefaultBookmarksPanel() {
|
32 |
this(null); |
33 |
} |
34 |
|
35 |
public DefaultBookmarksPanel(Expression expression) { |
36 |
this.state = STATE_VIEWING;
|
37 |
if( expression!=null ) { |
38 |
Date now = new Date(); |
39 |
String name = String.format( |
40 |
"%tF %tR %s",
|
41 |
now, now, |
42 |
StringUtils.abbreviate(expression.getPhrase(), 40)
|
43 |
); |
44 |
Bookmarks bookmarks = ExpressionEvaluatorLocator.getManager().getBookmarks(); |
45 |
Bookmark bookmark = bookmarks.add(name, expression); |
46 |
this.put(bookmark);
|
47 |
this.changeStateTo(STATE_MODIFYING);
|
48 |
} |
49 |
this.initComponents();
|
50 |
} |
51 |
|
52 |
private void initComponents() { |
53 |
this.currentIndex = -1; |
54 |
this.currentBookmark = null; |
55 |
this.lstBookmarks.setModel(this.createBookmarksModel()); |
56 |
this.lstBookmarks.addListSelectionListener(new ListSelectionListener() { |
57 |
@Override
|
58 |
public void valueChanged(ListSelectionEvent e) { |
59 |
if( e.getValueIsAdjusting()) {
|
60 |
return;
|
61 |
} |
62 |
if( state != STATE_VIEWING ) {
|
63 |
return;
|
64 |
} |
65 |
LabeledValue<Bookmark> value = (LabeledValue<Bookmark>) lstBookmarks.getSelectedValue(); |
66 |
if( value == null ) { |
67 |
return;
|
68 |
} |
69 |
put(value.getValue()); |
70 |
} |
71 |
}); |
72 |
this.btnDelete.addActionListener(new ActionListener() { |
73 |
@Override
|
74 |
public void actionPerformed(ActionEvent e) { |
75 |
doDelete(); |
76 |
} |
77 |
}); |
78 |
this.btnModify.addActionListener(new ActionListener() { |
79 |
@Override
|
80 |
public void actionPerformed(ActionEvent e) { |
81 |
doModify(); |
82 |
} |
83 |
}); |
84 |
this.btnAccept.addActionListener(new ActionListener() { |
85 |
@Override
|
86 |
public void actionPerformed(ActionEvent e) { |
87 |
doAccept(); |
88 |
} |
89 |
}); |
90 |
this.btnDiscard.addActionListener(new ActionListener() { |
91 |
@Override
|
92 |
public void actionPerformed(ActionEvent e) { |
93 |
doDiscard(); |
94 |
} |
95 |
}); |
96 |
this.txtLastUse.setEditable(false); |
97 |
this.txtExpression.setEditable(false); |
98 |
this.setPreferredSize(new Dimension(450, 350)); |
99 |
} |
100 |
|
101 |
private ListModel<Bookmark> createBookmarksModel() { |
102 |
Bookmarks bookmarks = ExpressionEvaluatorLocator.getManager().getBookmarks(); |
103 |
DefaultListModel<Bookmark> model = new DefaultListModel<>(); |
104 |
for (Bookmark bookmark : bookmarks) {
|
105 |
model.addElement(bookmark); |
106 |
} |
107 |
return model;
|
108 |
} |
109 |
|
110 |
public Bookmark getCurrentBookmark() {
|
111 |
return this.currentBookmark; |
112 |
} |
113 |
|
114 |
private void put(Bookmark bookmark) { |
115 |
this.currentBookmark = bookmark;
|
116 |
this.currentIndex = this.lstBookmarks.getSelectedIndex(); |
117 |
this.txtExpression.setText(bookmark.getValue().getPhrase());
|
118 |
this.txtExpression.setCaretPosition(0); |
119 |
this.txtName.setText(bookmark.getName());
|
120 |
this.txtLastUse.setText(String.valueOf(bookmark.getLastUse())); |
121 |
} |
122 |
|
123 |
private void fetch(Bookmark bookmark) { |
124 |
bookmark.setName(this.txtName.getText());
|
125 |
} |
126 |
|
127 |
private void doDelete() { |
128 |
Bookmark bookmark = (Bookmark) this.lstBookmarks.getSelectedValue();
|
129 |
if( bookmark==null ) { |
130 |
return;
|
131 |
} |
132 |
Bookmarks bookmarks = ExpressionEvaluatorLocator.getManager().getBookmarks(); |
133 |
bookmarks.remove(bookmark.getName()); |
134 |
this.lstBookmarks.setModel(this.createBookmarksModel()); |
135 |
this.changeStateTo(STATE_VIEWING);
|
136 |
} |
137 |
|
138 |
private void doModify() { |
139 |
Bookmark bookmark = (Bookmark) this.lstBookmarks.getSelectedValue();
|
140 |
if( bookmark==null ) { |
141 |
return;
|
142 |
} |
143 |
this.put(bookmark);
|
144 |
this.changeStateTo(STATE_MODIFYING);
|
145 |
} |
146 |
|
147 |
private void doAccept() { |
148 |
Bookmark bookmark = (Bookmark) this.lstBookmarks.getSelectedValue();
|
149 |
if( bookmark==null ) { |
150 |
return;
|
151 |
} |
152 |
this.fetch(bookmark);
|
153 |
this.changeStateTo(STATE_VIEWING);
|
154 |
} |
155 |
|
156 |
private void doDiscard() { |
157 |
Bookmark bookmark = (Bookmark) this.lstBookmarks.getSelectedValue();
|
158 |
if( bookmark==null ) { |
159 |
return;
|
160 |
} |
161 |
this.put(bookmark);
|
162 |
this.changeStateTo(STATE_VIEWING);
|
163 |
} |
164 |
|
165 |
private void changeStateTo(int newstate) { |
166 |
switch(newstate) {
|
167 |
case STATE_VIEWING:
|
168 |
this.btnAccept.setEnabled(false); |
169 |
this.btnDelete.setEnabled(true); |
170 |
this.btnDiscard.setEnabled(false); |
171 |
this.btnModify.setEnabled(true); |
172 |
this.txtName.setEnabled(false); |
173 |
this.txtLastUse.setEnabled(false); |
174 |
this.txtExpression.setEnabled(false); |
175 |
break;
|
176 |
case STATE_MODIFYING:
|
177 |
this.btnAccept.setEnabled(true); |
178 |
this.btnDelete.setEnabled(true); |
179 |
this.btnDiscard.setEnabled(true); |
180 |
this.btnModify.setEnabled(false); |
181 |
this.txtName.setEnabled(true); |
182 |
this.txtLastUse.setEnabled(true); |
183 |
this.txtExpression.setEnabled(true); |
184 |
break;
|
185 |
} |
186 |
this.state = newstate;
|
187 |
} |
188 |
} |