001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.properties.editors;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.awt.Dimension;
024: import java.awt.FlowLayout;
025: import java.awt.Toolkit;
026: import java.awt.datatransfer.Clipboard;
027: import java.awt.datatransfer.DataFlavor;
028: import java.awt.datatransfer.StringSelection;
029: import java.awt.datatransfer.Transferable;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.awt.event.MouseAdapter;
033: import java.awt.event.MouseEvent;
034: import java.util.Collection;
035: import java.util.Iterator;
036: import java.util.LinkedList;
037: import java.util.StringTokenizer;
038:
039: import javax.swing.DefaultListModel;
040: import javax.swing.JButton;
041: import javax.swing.JList;
042: import javax.swing.JOptionPane;
043: import javax.swing.JPanel;
044: import javax.swing.JScrollPane;
045:
046: import com.jeta.open.gui.components.BasicPopupMenu;
047: import com.jeta.open.gui.components.JETAComponentNames;
048: import com.jeta.open.gui.framework.JETAController;
049: import com.jeta.open.gui.framework.JETAPanel;
050: import com.jeta.open.gui.framework.UIDirector;
051: import com.jeta.open.i18n.I18N;
052: import com.jeta.open.support.CompositeComponentFinder;
053: import com.jeta.open.support.DefaultComponentFinder;
054: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
055: import com.jeta.swingbuilder.resources.Icons;
056:
057: /**
058: * This class displays a panel that allows a user to add and arrange String
059: * objects in a collection.
060: *
061: * @author Jeff Tassin
062: */
063: public class ItemsPanel extends JETAPanel {
064: public static final String ID_ADD_ITEM = "add.item";
065: public static final String ID_EDIT_ITEM = "edit.item";
066: public static final String ID_DELETE_ITEM = "delete.item";
067: public static final String ID_MOVE_UP = "move.up";
068: public static final String ID_MOVE_DOWN = "move.down";
069:
070: private JList m_list;
071: private DefaultListModel m_model;
072: private BasicPopupMenu m_copy_popup;
073:
074: /**
075: * ctor
076: */
077: public ItemsPanel() {
078: this (null);
079: }
080:
081: /**
082: * ctor
083: */
084: public ItemsPanel(Collection items) {
085: super (new CompositeComponentFinder());
086:
087: m_copy_popup = new BasicPopupMenu();
088:
089: CompositeComponentFinder finder = (CompositeComponentFinder) getComponentFinder();
090: finder.add(new DefaultComponentFinder(this ));
091: finder.add(new DefaultComponentFinder(m_copy_popup));
092:
093: setLayout(new BorderLayout());
094: add(createToolBar(), BorderLayout.NORTH);
095: add(createList(), BorderLayout.CENTER);
096: setController(new ItemsPanelController(this , m_copy_popup));
097:
098: if (items != null) {
099: Iterator iter = items.iterator();
100: while (iter.hasNext()) {
101: m_model.addElement(iter.next());
102: }
103: }
104: }
105:
106: /**
107: * Creates a button for the toolbar
108: */
109: private Component _createButton(String imageName, String cmdId) {
110: JButton btn = new JButton(FormDesignerUtils
111: .loadImage(imageName));
112: btn.setActionCommand(cmdId);
113: btn.setName(cmdId);
114: Dimension d = new Dimension(24, 24);
115: btn.setPreferredSize(d);
116: btn.setMaximumSize(d);
117: return btn;
118: }
119:
120: /**
121: * Creates the toolbar at the top of the panel
122: */
123: private Component createToolBar() {
124: JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
125:
126: panel.add(_createButton(Icons.NEW_16, ID_ADD_ITEM));
127: panel.add(_createButton(Icons.EDIT_16, ID_EDIT_ITEM));
128: panel.add(_createButton(Icons.DELETE_16, ID_DELETE_ITEM));
129: panel.add(_createButton(Icons.UP_16, ID_MOVE_UP));
130: panel.add(_createButton(Icons.DOWN_16, ID_MOVE_DOWN));
131: return panel;
132: }
133:
134: /**
135: * Creates the list that displays the items in the collection we are
136: * editing.
137: */
138: private Component createList() {
139: m_list = new JList();
140: m_model = new DefaultListModel();
141: m_list.setModel(m_model);
142: JScrollPane scroll = new JScrollPane(m_list);
143: return scroll;
144: }
145:
146: /**
147: * @return the items in the list
148: */
149: public Collection getItems() {
150: return java.util.Collections.list(m_model.elements());
151: }
152:
153: /**
154: * @return the underlying list
155: */
156: public JList getList() {
157: return m_list;
158: }
159:
160: /**
161: * @return the underlying list model
162: */
163: public DefaultListModel getListModel() {
164: return m_model;
165: }
166:
167: /**
168: * @return the preferred size for this component
169: */
170: public Dimension getPreferredSize() {
171: return FormDesignerUtils.getWindowDimension(this , 175, 144);
172: }
173:
174: /**
175: * Controller for this panel
176: */
177: public static class ItemsPanelController extends JETAController {
178: /**
179: * The panel we are controlling
180: */
181: private ItemsPanel m_panel;
182:
183: private BasicPopupMenu m_copy_popup;
184:
185: /**
186: * ctor
187: */
188: public ItemsPanelController(ItemsPanel panel,
189: BasicPopupMenu popup) {
190: super (panel);
191: m_panel = panel;
192: m_copy_popup = popup;
193:
194: assignAction(ItemsPanel.ID_ADD_ITEM, new AddItemAction());
195: assignAction(ItemsPanel.ID_EDIT_ITEM, new EditItemAction());
196: assignAction(ItemsPanel.ID_DELETE_ITEM,
197: new DeleteItemAction());
198: assignAction(ItemsPanel.ID_MOVE_UP, new MoveUpAction());
199: assignAction(ItemsPanel.ID_MOVE_DOWN, new MoveDownAction());
200:
201: assignAction(JETAComponentNames.ID_CUT, new CutAction());
202: assignAction(JETAComponentNames.ID_COPY, new CopyAction());
203: assignAction(JETAComponentNames.ID_PASTE, new PasteAction());
204: // setUIDirector( new ItemsPanelUIDirector( m_panel ) );
205:
206: m_panel.getList().addMouseListener(new MouseAdapter() {
207: public void mousePressed(MouseEvent evt) {
208: if (evt.isPopupTrigger()) {
209: m_copy_popup.show(m_panel.getList(),
210: evt.getX(), evt.getY());
211: }
212: }
213: });
214: }
215:
216: void ensureIndexIsVisible() {
217: JList list = m_panel.getList();
218: int index = list.getSelectedIndex();
219: if (index >= 0)
220: list.ensureIndexIsVisible(index);
221: }
222:
223: public class AddItemAction implements ActionListener {
224: public void actionPerformed(ActionEvent evt) {
225: String item = JOptionPane.showInputDialog(I18N
226: .getLocalizedMessage("Enter New Item"));
227: if (item != null) {
228: JList list = m_panel.getList();
229: int index = list.getSelectedIndex();
230: DefaultListModel model = m_panel.getListModel();
231: if (index >= 0 && index < (model.size() - 1)) {
232: model.insertElementAt(item, index + 1);
233: list.setSelectedIndex(index + 1);
234: } else {
235: model.addElement(item);
236: list.setSelectedIndex(model.size() - 1);
237: }
238: }
239: ensureIndexIsVisible();
240: }
241: }
242:
243: public class CutAction implements ActionListener {
244: public void actionPerformed(ActionEvent evt) {
245: invokeAction(JETAComponentNames.ID_COPY);
246: invokeAction(ItemsPanel.ID_DELETE_ITEM);
247: }
248: }
249:
250: public class CopyAction implements ActionListener {
251: public void actionPerformed(ActionEvent evt) {
252: StringBuffer sbuff = new StringBuffer();
253: JList list = m_panel.getList();
254: DefaultListModel model = (DefaultListModel) list
255: .getModel();
256: int[] indexes = list.getSelectedIndices();
257: for (int i = 0; i < indexes.length; i++) {
258: sbuff.append(model.elementAt(indexes[i]));
259: if ((i + 1) < indexes.length)
260: sbuff.append("\n");
261: }
262:
263: Toolkit kit = Toolkit.getDefaultToolkit();
264: Clipboard clipboard = kit.getSystemClipboard();
265: StringSelection transferable = new StringSelection(
266: sbuff.toString());
267: clipboard.setContents(transferable, null);
268: }
269: }
270:
271: public class PasteAction implements ActionListener {
272: public void actionPerformed(ActionEvent evt) {
273: try {
274: JList list = m_panel.getList();
275: DefaultListModel model = (DefaultListModel) list
276: .getModel();
277: int index = list.getSelectedIndex();
278: if (index < 0)
279: index = 0;
280:
281: Toolkit kit = Toolkit.getDefaultToolkit();
282: Clipboard clipboard = kit.getSystemClipboard();
283: Transferable transferable = clipboard
284: .getContents(null);
285: if (transferable
286: .isDataFlavorSupported(DataFlavor.stringFlavor)) {
287: String sval = (String) transferable
288: .getTransferData(DataFlavor.stringFlavor);
289: StringTokenizer st = new StringTokenizer(sval,
290: "\t\n\r\f");
291: while (st.hasMoreTokens()) {
292: String token = st.nextToken();
293: model.insertElementAt(token, index);
294: index++;
295: }
296: }
297: } catch (Exception e) {
298:
299: }
300: }
301: }
302:
303: public class EditItemAction implements ActionListener {
304: public void actionPerformed(ActionEvent evt) {
305: JList list = m_panel.getList();
306: int index = list.getSelectedIndex();
307: if (index >= 0) {
308: DefaultListModel model = m_panel.getListModel();
309: String item = JOptionPane.showInputDialog(I18N
310: .getLocalizedMessage("Enter New Item"),
311: model.elementAt(index));
312: if (item != null) {
313: model.setElementAt(item, index);
314: }
315: }
316: ensureIndexIsVisible();
317:
318: }
319: }
320:
321: public class DeleteItemAction implements ActionListener {
322: public void actionPerformed(ActionEvent evt) {
323: LinkedList del_items = new LinkedList();
324: JList list = m_panel.getList();
325: int[] indexes = list.getSelectedIndices();
326: DefaultListModel model = m_panel.getListModel();
327: for (int i = 0; i < indexes.length; i++) {
328: del_items.add(model.elementAt(indexes[i]));
329: }
330:
331: Iterator iter = del_items.iterator();
332: while (iter.hasNext()) {
333: model.removeElement(iter.next());
334: }
335: int index = indexes[0];
336: index--;
337: if (index < 0)
338: index = 0;
339: if (model.size() > 0)
340: list.setSelectedIndex(index);
341: ensureIndexIsVisible();
342: }
343: }
344:
345: public class MoveUpAction implements ActionListener {
346: public void actionPerformed(ActionEvent evt) {
347: JList list = m_panel.getList();
348: DefaultListModel model = m_panel.getListModel();
349: int index = list.getSelectedIndex();
350: System.out.println("MoveUP action index: " + index);
351:
352: if (index > 0 && model.size() > 1) {
353: Object mv_obj = model.getElementAt(index);
354: model.removeElementAt(index);
355: model.add(index - 1, mv_obj);
356: list.setSelectedIndex(index - 1);
357: }
358: ensureIndexIsVisible();
359:
360: }
361: }
362:
363: public class MoveDownAction implements ActionListener {
364: public void actionPerformed(ActionEvent evt) {
365: JList list = m_panel.getList();
366: DefaultListModel model = m_panel.getListModel();
367: int index = list.getSelectedIndex();
368: if ((index + 1) < model.size()) {
369: Object mv_obj = model.getElementAt(index);
370: model.removeElementAt(index);
371: if ((index + 1) == model.size()) {
372: model.addElement(mv_obj);
373: } else {
374: model.add(index + 1, mv_obj);
375: }
376: list.setSelectedIndex(index + 1);
377: }
378: ensureIndexIsVisible();
379:
380: }
381: }
382: }
383:
384: public static class ItemsPanelUIDirector implements UIDirector {
385: /**
386: * The panel we are controlling
387: */
388: private ItemsPanel m_panel;
389:
390: /**
391: * ctor
392: */
393: public ItemsPanelUIDirector(ItemsPanel panel) {
394: m_panel = panel;
395: }
396:
397: public void updateComponents(java.util.EventObject evt) {
398:
399: }
400: }
401:
402: }
|