001: /*
002: JSmooth: a VM wrapper toolkit for Windows
003: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the GNU General Public License as published by
007: the Free Software Foundation; either version 2 of the License, or
008: (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:
019: */
020:
021: package net.charabia.jsmoothgen.application.gui.util;
022:
023: import javax.swing.*;
024:
025: public class SortedEditableList extends javax.swing.JPanel {
026: private boolean m_editableItems = true;
027: private Editor m_editor;
028:
029: private DefaultListModel m_model = new DefaultListModel();
030:
031: public interface Editor {
032: public Object createNewItem(SortedEditableList selist);
033:
034: public Object editItem(SortedEditableList selist, Object item);
035:
036: public boolean removeItem(SortedEditableList selist, Object item);
037: }
038:
039: public class StringEditor implements SortedEditableList.Editor {
040: public Object createNewItem(SortedEditableList selist) {
041: String result = JOptionPane.showInputDialog(selist,
042: "Type a string");
043: return result;
044: }
045:
046: public Object editItem(SortedEditableList selist, Object item) {
047: String result = JOptionPane.showInputDialog(selist,
048: "Type a string", item.toString());
049: return result;
050: }
051:
052: public boolean removeItem(SortedEditableList selist, Object item) {
053: return true;
054: }
055: }
056:
057: /** Creates new form BeanForm */
058: public SortedEditableList() {
059: m_editor = new SortedEditableList.StringEditor();
060: initComponents();
061: m_itemList.setModel(m_model);
062: }
063:
064: public void setEditor(SortedEditableList.Editor editor) {
065: m_editor = editor;
066: }
067:
068: public void setData(Object[] items) {
069: m_model.removeAllElements();
070: for (int i = 0; i < items.length; i++)
071: m_model.addElement(items[i]);
072: }
073:
074: public Object[] getData() {
075: return m_model.toArray();
076: }
077:
078: public int dataSize() {
079: return m_model.size();
080: }
081:
082: /** This method is called from within the constructor to
083: * initialize the form.
084: * WARNING: Do NOT modify this code. The content of this method is
085: * always regenerated by the Form Editor.
086: */
087: private void initComponents()//GEN-BEGIN:initComponents
088: {
089: java.awt.GridBagConstraints gridBagConstraints;
090:
091: jScrollPane1 = new javax.swing.JScrollPane();
092: m_itemList = new javax.swing.JList();
093: m_buttonAdd = new javax.swing.JButton();
094: m_buttonRemove = new javax.swing.JButton();
095: m_buttonEdit = new javax.swing.JButton();
096: jSeparator1 = new javax.swing.JSeparator();
097: m_buttonUp = new javax.swing.JButton();
098: m_buttonDown = new javax.swing.JButton();
099:
100: setLayout(new java.awt.GridBagLayout());
101:
102: jScrollPane1.setViewportView(m_itemList);
103:
104: gridBagConstraints = new java.awt.GridBagConstraints();
105: gridBagConstraints.gridx = 1;
106: gridBagConstraints.gridwidth = 4;
107: gridBagConstraints.gridheight = 8;
108: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
109: gridBagConstraints.weightx = 1.0;
110: gridBagConstraints.weighty = 1.0;
111: add(jScrollPane1, gridBagConstraints);
112:
113: m_buttonAdd.setIcon(new javax.swing.ImageIcon(getClass()
114: .getResource("/icons/stock_insert-element.png")));
115: m_buttonAdd
116: .addActionListener(new java.awt.event.ActionListener() {
117: public void actionPerformed(
118: java.awt.event.ActionEvent evt) {
119: buttonAddActionPerformed(evt);
120: }
121: });
122:
123: gridBagConstraints = new java.awt.GridBagConstraints();
124: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
125: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
126: add(m_buttonAdd, gridBagConstraints);
127:
128: m_buttonRemove.setIcon(new javax.swing.ImageIcon(getClass()
129: .getResource("/icons/stock_remove-element.png")));
130: m_buttonRemove
131: .addActionListener(new java.awt.event.ActionListener() {
132: public void actionPerformed(
133: java.awt.event.ActionEvent evt) {
134: buttonRemoveActionPerformed(evt);
135: }
136: });
137:
138: gridBagConstraints = new java.awt.GridBagConstraints();
139: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
140: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
141: add(m_buttonRemove, gridBagConstraints);
142:
143: m_buttonEdit.setIcon(new javax.swing.ImageIcon(getClass()
144: .getResource("/icons/stock_edit.png")));
145: m_buttonEdit
146: .addActionListener(new java.awt.event.ActionListener() {
147: public void actionPerformed(
148: java.awt.event.ActionEvent evt) {
149: buttonEditActionPerformed(evt);
150: }
151: });
152:
153: gridBagConstraints = new java.awt.GridBagConstraints();
154: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
155: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
156: add(m_buttonEdit, gridBagConstraints);
157:
158: gridBagConstraints = new java.awt.GridBagConstraints();
159: gridBagConstraints.weighty = 1.0;
160: gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
161: add(jSeparator1, gridBagConstraints);
162:
163: m_buttonUp.setIcon(new javax.swing.ImageIcon(getClass()
164: .getResource("/icons/stock_up.png")));
165: m_buttonUp
166: .addActionListener(new java.awt.event.ActionListener() {
167: public void actionPerformed(
168: java.awt.event.ActionEvent evt) {
169: buttonUpActionPerformed(evt);
170: }
171: });
172:
173: gridBagConstraints = new java.awt.GridBagConstraints();
174: gridBagConstraints.gridy = 6;
175: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
176: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
177: gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
178: add(m_buttonUp, gridBagConstraints);
179:
180: m_buttonDown.setIcon(new javax.swing.ImageIcon(getClass()
181: .getResource("/icons/stock_down.png")));
182: m_buttonDown
183: .addActionListener(new java.awt.event.ActionListener() {
184: public void actionPerformed(
185: java.awt.event.ActionEvent evt) {
186: buttonDownActionPerformed(evt);
187: }
188: });
189:
190: gridBagConstraints = new java.awt.GridBagConstraints();
191: gridBagConstraints.gridy = 7;
192: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
193: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
194: gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
195: add(m_buttonDown, gridBagConstraints);
196:
197: }//GEN-END:initComponents
198:
199: private void buttonDownActionPerformed(
200: java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttonDownActionPerformed
201: {//GEN-HEADEREND:event_buttonDownActionPerformed
202: // Add your handling code here:
203: int max = m_model.size();
204: int index = m_itemList.getSelectedIndex();
205: if ((index + 1) < max) {
206: Object o = m_model.remove(index);
207: m_model.add(index + 1, o);
208: m_itemList.setSelectedIndex(index + 1);
209: }
210: modelChanged();
211: }//GEN-LAST:event_buttonDownActionPerformed
212:
213: private void buttonUpActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttonUpActionPerformed
214: {//GEN-HEADEREND:event_buttonUpActionPerformed
215: // Add your handling code here:
216: int index = m_itemList.getSelectedIndex();
217: if (index > 0) {
218: Object o = m_model.remove(index);
219: m_model.add(index - 1, o);
220: m_itemList.setSelectedIndex(index - 1);
221: }
222: modelChanged();
223: }//GEN-LAST:event_buttonUpActionPerformed
224:
225: public void setEditableItems(boolean b) {
226: m_editableItems = b;
227: m_buttonEdit.setEnabled(b);
228: m_buttonAdd.setEnabled(b);
229: m_buttonRemove.setEnabled(b);
230: }
231:
232: public boolean getEditableItems() {
233: return m_editableItems;
234: }
235:
236: private void buttonEditActionPerformed(
237: java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttonEditActionPerformed
238: {//GEN-HEADEREND:event_buttonEditActionPerformed
239: // Add your handling code here:
240: int index = m_itemList.getSelectedIndex();
241: if (index >= 0) {
242: Object o = m_model.get(index);
243: Object n = m_editor.editItem(this , o);
244: if (n != null) {
245: m_model.removeElementAt(index);
246: m_model.add(index, n);
247: m_itemList.setSelectedIndex(index);
248: }
249: }
250: modelChanged();
251: }//GEN-LAST:event_buttonEditActionPerformed
252:
253: private void buttonRemoveActionPerformed(
254: java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttonRemoveActionPerformed
255: {//GEN-HEADEREND:event_buttonRemoveActionPerformed
256: // Add your handling code here:
257:
258: Object[] items = m_itemList.getSelectedValues();
259: for (int i = 0; i < items.length; i++) {
260: Object o = items[i];
261: if (o != null) {
262: if (m_editor.removeItem(this , o))
263: m_model.removeElement(o);
264: }
265: }
266: modelChanged();
267: }//GEN-LAST:event_buttonRemoveActionPerformed
268:
269: private void buttonAddActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_buttonAddActionPerformed
270: {//GEN-HEADEREND:event_buttonAddActionPerformed
271: // Add your handling code here:
272: Object item = m_editor.createNewItem(this );
273: if (item != null) {
274: System.out.println("IsArray: " + item.getClass() + " == "
275: + item.getClass().isArray());
276: if (item.getClass().isArray()) {
277: for (int i = 0; i < java.lang.reflect.Array
278: .getLength(item); i++) {
279: Object o = java.lang.reflect.Array.get(item, i);
280: if (m_model.contains(o) == false)
281: m_model.addElement(o);
282: }
283: } else {
284: if (m_model.contains(item) == false)
285: m_model.addElement(item);
286: }
287: modelChanged();
288: }
289: }//GEN-LAST:event_buttonAddActionPerformed
290:
291: protected void modelChanged() {
292: }
293:
294: // Variables declaration - do not modify//GEN-BEGIN:variables
295: private javax.swing.JScrollPane jScrollPane1;
296: private javax.swing.JSeparator jSeparator1;
297: private javax.swing.JButton m_buttonAdd;
298: private javax.swing.JButton m_buttonDown;
299: private javax.swing.JButton m_buttonEdit;
300: private javax.swing.JButton m_buttonRemove;
301: private javax.swing.JButton m_buttonUp;
302: private javax.swing.JList m_itemList;
303: // End of variables declaration//GEN-END:variables
304:
305: }
|