001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.demo;
022:
023: import java.awt.BorderLayout;
024: import java.awt.Color;
025: import java.awt.Dimension;
026: import java.awt.GridBagConstraints;
027: import java.awt.GridBagLayout;
028: import java.awt.GridLayout;
029: import java.awt.Insets;
030: import java.awt.Point;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.awt.event.ItemEvent;
034: import java.awt.event.ItemListener;
035: import com.javujavu.javux.wings.RadioGroup;
036: import com.javujavu.javux.wings.WingButton;
037: import com.javujavu.javux.wings.WingCheckBox;
038: import com.javujavu.javux.wings.WingCombo;
039: import com.javujavu.javux.wings.WingComponent;
040: import com.javujavu.javux.wings.WingImage;
041: import com.javujavu.javux.wings.WingLabel;
042: import com.javujavu.javux.wings.WingPanel;
043: import com.javujavu.javux.wings.WingScrollPane;
044: import com.javujavu.javux.wings.WingSkin;
045: import com.javujavu.javux.wings.WingSpinner;
046: import com.javujavu.javux.wings.WingSplitPane;
047: import com.javujavu.javux.wings.WingTable;
048: import com.javujavu.javux.wings.extra.ColorItem;
049: import com.javujavu.javux.wings.item.DefaultItemEditor;
050: import com.javujavu.javux.wings.item.ItemOwner;
051: import com.javujavu.javux.wings.item.LabelItem;
052:
053: public class TablePanel extends WingPanel implements ActionListener,
054: ItemListener {
055: private static String[] title = { "Truman Show", "Rambo",
056: "Moonraker", "Platoon", "Predator", "Star Wars", "Zorro" };
057: private static String[] first = { "Hannibal", "John", "James",
058: "Donald", "Austin", "Lara", "Wilma", "Robin", "Truman",
059: "Hannibal" };
060: private static String[] last = { "Lecter", "Rambo", "Bond", "Duck",
061: "Powers", "Croft", "Flinstone", "Hood", "Burbank" };
062:
063: private static ColorItem[] color = {
064: new ColorItem("red", Color.red),
065: new ColorItem("blue", Color.blue),
066: new ColorItem("green", Color.green),
067: new ColorItem("orange", Color.orange),
068: new ColorItem("pink", Color.pink),
069: new ColorItem("magenta", Color.magenta),
070: new ColorItem("yellow", Color.yellow) };
071: private static WingImage[] icons = {
072: WingSkin.getImage("imgSmile", null),
073: WingSkin.getImage("imgTongue", null),
074: WingSkin.getImage("imgSilence", null),
075: WingSkin.getImage("imgGlasses", null),
076: WingSkin.getImage("imgUgly", null),
077: WingSkin.getImage("imgYawn", null),
078: WingSkin.getImage("imgBomb", null),
079: WingSkin.getImage("imgScared", null),
080: WingSkin.getImage("imgSeek", null),
081: WingSkin.getImage("imgCrying", null) };
082: private static Object[] misc = {
083: new LabelItem("blah blah", WingSkin.getImage("imgTongue",
084: null)),
085: new LabelItem("smile", WingSkin.getImage("imgSmile", null)),
086: new LabelItem("silence", WingSkin.getImage("imgSilence",
087: null)), "blah blah blah",
088: new ColorItem("color green", Color.green),
089: WingSkin.getImage("imgGlasses", null), new Boolean(true),
090: new ColorItem("color red", Color.red), };
091:
092: private WingCheckBox cbCellSelection;
093: private WingTable tb1;
094: private WingTable tb2;
095: private WingTable tb3;
096: private WingCheckBox rbSingleSelection;
097: private WingCheckBox rbContinousSelection;
098: private WingCheckBox rbMultipleSelection;
099: private WingCheckBox cbEditable;
100: private WingTable target;
101: private WingButton bAdd;
102: private WingButton bInsert;
103: private WingButton bRemove;
104:
105: public TablePanel(WingSetPanel owner) {
106: this .setLayout(new BorderLayout());
107:
108: WingSplitPane sp0, sp1;
109: this
110: .add(sp0 = new WingSplitPane(VERTICAL),
111: BorderLayout.CENTER);
112: sp0.setResizeWeight(0.5);
113: sp0.setTopComponent(tb1 = makeTable());
114: sp0.setBottomComponent(sp1 = new WingSplitPane(HORIZONTAL));
115: WingScrollPane sp2, sp3;
116: sp1
117: .setLeftComponent(sp2 = new WingScrollPane(
118: tb2 = makeTable()));
119: tb2.setStyleId("custom_table");
120: sp2.setPreferredSize(new Dimension(100, 130));
121: sp1.setResizeWeight(0.5);
122: sp1.setRightComponent(sp3 = new WingScrollPane(
123: tb3 = makeTable()));
124: tb3.setEnabled(false);
125: sp3.setPreferredSize(new Dimension(100, 130));
126:
127: String keys = "\n\nKeys:\nLeft,Right,Up,Down,Home,End - move\nShift+Left,Right,Up,Down,Home,End - select\nCtrl+A - select All\nCtrl+Insert,Ctrl+C - copy selection to clipboard\nEnter, F2, mouse doubleclick - open editor\nEnter,Esc - close editor";
128: tb1.setTooltip(new LabelItem("WingTable\ndefault style" + keys,
129: WingSet.imgGlasses, LEFT, RIGHT));
130: tb2
131: .setTooltip(new LabelItem(
132: "WingTable inside WingScrollPane\nstyle: custom_table (stylesheet wingsetdemo.ini)"
133: + keys, WingSet.imgGlasses, LEFT, RIGHT));
134: tb3.setTooltip(new LabelItem(
135: "disabled WingTable inside WingScrollPane\ndefault style"
136: + keys, WingSet.imgGlasses, LEFT, RIGHT));
137:
138: WingPanel p2, p3;
139: this .add(p2 = new WingPanel(new GridBagLayout()),
140: BorderLayout.SOUTH);
141: GridBagConstraints c = new GridBagConstraints();
142: c.gridwidth = 1;
143: c.weightx = 1;
144: c.insets = new Insets(3, 0, 0, 0);
145:
146: WingButton b;
147: p2.add(b = new WingButton("TopStyle"));
148: b.setStyleId("vpopup");
149: b.setTextPosition(LEFT);
150: b.setFastAction(true);
151: b
152: .setTooltip(new LabelItem(
153: "modify TopStyle of this WingTable\nTopStyle is an extra style overriding stylesheet settings \ndynamically at run time\nclick button to change settings",
154: WingSet.imgIcon, LEFT, RIGHT));
155: new TopStyleEditor(b, tb2, 0, 0, false);
156:
157: c.gridwidth = GridBagConstraints.REMAINDER;
158: p2.add(p3 = new WingPanel(new GridLayout(1, 0)), c);
159: p3.add(bAdd = new WingButton("add row"));
160: p3.add(bInsert = new WingButton("insert row"));
161: p3.add(bRemove = new WingButton("remove selected rows"));
162:
163: c.insets = new Insets(0, 0, 0, 0);
164: c.gridwidth = 1;
165: p2.add(cbCellSelection = new WingCheckBox("cell selection"), c);
166: cbCellSelection.setSelected(true);
167: c.gridwidth = GridBagConstraints.REMAINDER;
168: p2.add(p3 = new WingPanel(new GridLayout(1, 0)), c);
169: p3.add(rbSingleSelection = new WingCheckBox("single selection",
170: RADIO));
171: p3.add(rbContinousSelection = new WingCheckBox(
172: "continous selection", RADIO));
173: p3.add(rbMultipleSelection = new WingCheckBox(
174: "multiple selection", RADIO));
175: RadioGroup cg = new RadioGroup();
176: cg.add(rbSingleSelection);
177: cg.add(rbContinousSelection);
178: cg.add(rbMultipleSelection);
179: rbMultipleSelection.setSelected(true);
180:
181: c.gridwidth = 1;
182: p2
183: .add(cbEditable = new WingCheckBox("editable tables",
184: TOGGLE), c);
185: cbEditable.setSelected(true);
186: c.gridwidth = GridBagConstraints.REMAINDER;
187: p2
188: .add(
189: new WingLabel(
190: "TIP: doubleclick, Enter, F2 - open editor ; Esc, Enter - close ; Ctrl+Insert - copy to clipboard."),
191: c);
192:
193: this .addActionListener(this );
194: this .addItemListener(this );
195: }
196:
197: protected static WingTable makeTable() {
198: WingTable t = new WingTable();
199: t.setCellSelectionEnabled(true);
200: t.setSelectionMode(WingTable.MULTIPLE_SELECTION);
201: t.setEditable(true);
202: t.setEditor(new MyTableItemEditor());
203:
204: t.addColumn("title", 100, LEFT, null, null);
205: t.addColumn("first", 0, LEFT, null, null);
206: t.addColumn("last", 0, LEFT, null, null);
207: t.addColumn("belt", 0, CENTER, null, null);
208: t.addColumn("icon", 40, CENTER, null, null);
209: t.addColumn("credit", 0, RIGHT, null, null);
210: t.addColumn("mark", 30, CENTER, null, null);
211: t.addColumn("misc", 0, LEFT, null, null);
212:
213: for (int i = 0; i < 6; i++) {
214: t.addRow(makeRow(i));
215: }
216: return t;
217: }
218:
219: private static Object[] makeRow(int i) {
220: Object[] r = new Object[8];
221: r[0] = title[i > 0 ? (int) (Math.random() * title.length) : 0];
222: r[1] = first[(int) (Math.random() * first.length)];
223: r[2] = last[(int) (Math.random() * last.length)];
224: r[3] = color[(int) (Math.random() * color.length)];
225: r[4] = icons[(int) (Math.random() * icons.length)];
226: r[5] = new Integer((int) (Math.random() * 1000));
227: r[6] = new Boolean((Math.random() < 0.5));
228: r[7] = misc[(int) (Math.random() * misc.length)];
229: return r;
230: }
231:
232: public void itemStateChanged(ItemEvent e) {
233: Object src = e.getSource();
234: if (src == cbCellSelection) {
235: tb1
236: .setCellSelectionEnabled(e.getStateChange() == ItemEvent.SELECTED);
237: tb2
238: .setCellSelectionEnabled(e.getStateChange() == ItemEvent.SELECTED);
239: tb3
240: .setCellSelectionEnabled(e.getStateChange() == ItemEvent.SELECTED);
241: } else if (src == rbSingleSelection
242: || src == rbContinousSelection
243: || src == rbMultipleSelection) {
244: int sm = rbSingleSelection.isSelected() ? SINGLE_SELECTION
245: : rbContinousSelection.isSelected() ? CONTINUOUS_SELECTION
246: : MULTIPLE_SELECTION;
247: tb1.setSelectionMode(sm);
248: tb2.setSelectionMode(sm);
249: tb3.setSelectionMode(sm);
250: } else if (src == cbEditable) {
251: tb1.setEditable(e.getStateChange() == ItemEvent.SELECTED);
252: tb2.setEditable(e.getStateChange() == ItemEvent.SELECTED);
253: } else {
254: if (src == tb1)
255: target = tb1;
256: else if (src == tb2)
257: target = tb2;
258: }
259: }
260:
261: public void actionPerformed(ActionEvent e) {
262: Object src = e.getSource();
263: if (target == null)
264: target = tb2;
265: if (src == bAdd) {
266: target.addRow(makeRow(5));
267: } else if (src == bInsert) {
268: int i = target.getSelectedRow();
269: if (i == -1)
270: i = 0;
271: target.insertRow(i, makeRow(5));
272: } else if (src == bRemove) {
273: int row = target.getSelectedRow();
274: int rowCount = target.getSelectedRowCount();
275: int col = target.getSelectedColumn();
276: int colCount = target.getSelectedColumnCount();
277: boolean mask[] = new boolean[rowCount];
278:
279: for (int i = 0; i < rowCount; i++)
280: for (int j = 0; !mask[i] && j < colCount; j++)
281: mask[i] = target.isCellSelected(row + i, col + j);
282:
283: for (int i = 0; i < rowCount; i++) {
284: if (mask[i])
285: target.removeRow(row);
286: else
287: row++;
288: }
289: }
290: }
291:
292: public static class MyTableItemEditor extends DefaultItemEditor {
293: public WingComponent getComponent(ItemOwner owner,
294: Object value, Object context) {
295: int column = ((Point) context).x;
296: if (column == 1) {
297: WingCombo editor = getComboEditor(owner, false);
298: fillCombo(editor, 1);
299: editor.setSelectedItem(value);
300: return editor;
301: }
302: if (column == 2) {
303: WingCombo editor = getComboEditor(owner, true);
304: fillCombo(editor, 2);
305: editor.setSelectedItem(value);
306: return editor;
307:
308: }
309: if (column == 3) {
310: WingCombo editor = getComboEditor(owner, false);
311: fillCombo(editor, 3);
312: editor.setSelectedItem(value);
313: return editor;
314: }
315: if (column == 4) {
316: WingCombo editor = getComboEditor(owner, false);
317: fillCombo(editor, 4);
318: editor.setSelectedItem(value);
319: return editor;
320: }
321: if (column == 5) {
322: WingSpinner editor = (WingSpinner) super .getComponent(
323: owner, value, context);
324: editor.setHorizontalAlignment(RIGHT);
325: return editor;
326: }
327: if (column == 7) {
328: WingCombo editor = getComboEditor(owner, true);
329: fillCombo(editor, 7);
330: editor.setSelectedItem(value);
331: return editor;
332: }
333: return super .getComponent(owner, value, context);
334: }
335: }
336:
337: public static void fillCombo(WingCombo cb, int i) {
338: Object[] data = (i == 1) ? first : (i == 2) ? last
339: : (i == 3) ? color : (i == 4) ? icons : misc;
340: for (int j = 0; j < data.length; j++)
341: cb.addItem(data[j]);
342: }
343: }
|