001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.properties.editors;
015:
016: import org.jetbrains.annotations.NonNls;
017: import org.jetbrains.annotations.NotNull;
018: import org.jetbrains.annotations.Nullable;
019: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
020: import org.pentaho.reportdesigner.lib.client.util.UIConstants;
021:
022: import javax.swing.*;
023: import javax.swing.border.Border;
024: import javax.swing.table.TableCellEditor;
025: import javax.swing.tree.TreeCellEditor;
026: import java.awt.*;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.awt.event.ItemEvent;
030: import java.awt.event.ItemListener;
031: import java.awt.event.MouseEvent;
032: import java.io.Serializable;
033: import java.net.URL;
034: import java.util.EventObject;
035: import java.util.HashSet;
036: import java.util.logging.Level;
037: import java.util.logging.Logger;
038:
039: public class CommonCellEditor extends AbstractCellEditor implements
040: TableCellEditor, TreeCellEditor {
041: @NonNls
042: @NotNull
043: private static final Logger LOG = Logger
044: .getLogger(CommonCellEditor.class.getName());
045:
046: @NotNull
047: protected JComponent editorComponent;
048: @NotNull
049: protected EditorDelegate delegate;
050:
051: protected int clickCountToStart = 1;
052:
053: private static final boolean DRAG_FIX = false;
054:
055: public enum TextFieldType {
056: @NotNull
057: INTEGER, @NotNull
058: LONG, @NotNull
059: FLOAT, @NotNull
060: DOUBLE, @NotNull
061: TEXT, @NotNull
062: URL
063: }
064:
065: @SuppressWarnings({"RawUseOfParameterizedType"})
066: public CommonCellEditor(@NotNull
067: final CellEditorJTextFieldWithEllipsis textFieldWithEllipsis) {
068: editorComponent = textFieldWithEllipsis;
069: this .clickCountToStart = 1;
070: delegate = new EditorDelegate() {
071: public void setValue(@Nullable
072: Object value) {
073: Border border = UIManager
074: .getBorder(UIConstants.TABLE_FOCUS_CELL_HIGHLIGHT_BORDER);
075: textFieldWithEllipsis.setBorder(border);
076: //noinspection unchecked
077: textFieldWithEllipsis.setValue(value, true);
078: }
079:
080: @Nullable
081: public Object getCellEditorValue() {
082: return textFieldWithEllipsis.getValue();
083: }
084: };
085:
086: textFieldWithEllipsis.getEllipsisButton().addActionListener(
087: delegate);
088: textFieldWithEllipsis.getTextField()
089: .addActionListener(delegate);
090: }
091:
092: @SuppressWarnings({"RawUseOfParameterizedType"})
093: public CommonCellEditor(@NotNull
094: final CellEditorJLabelWithEllipsis labelWithEllipsis) {
095: editorComponent = labelWithEllipsis;
096: this .clickCountToStart = 1;
097: delegate = new EditorDelegate() {
098: public void setValue(@Nullable
099: Object value) {
100: Border border = UIManager
101: .getBorder(UIConstants.TABLE_FOCUS_CELL_HIGHLIGHT_BORDER);
102: labelWithEllipsis.setBorder(border);
103: //noinspection unchecked
104: labelWithEllipsis.setValue(value, true);
105: }
106:
107: @Nullable
108: public Object getCellEditorValue() {
109: return labelWithEllipsis.getValue();
110: }
111: };
112:
113: labelWithEllipsis.getEllipsisButton().addActionListener(
114: delegate);
115: }
116:
117: public CommonCellEditor(@NotNull
118: final JTextField textField, @NotNull
119: final TextFieldType textFieldType) {
120: editorComponent = textField;
121: this .clickCountToStart = 2;
122: delegate = new EditorDelegate() {
123: @Nullable
124: private Object origValue;
125:
126: public void setValue(@Nullable
127: Object value) {
128: Border border = UIManager
129: .getBorder(UIConstants.TABLE_FOCUS_CELL_HIGHLIGHT_BORDER);
130: textField.setBorder(border);
131: textField.setText((value != null) ? value.toString()
132: : "");
133: origValue = value;
134: }
135:
136: @Nullable
137: public Object getCellEditorValue() {
138: if (textFieldType == TextFieldType.INTEGER) {
139: if (textField.getText().length() == 0) {
140: return null;
141: }
142:
143: try {
144: return Integer.valueOf(textField.getText());
145: } catch (Exception e) {
146: if (LOG.isLoggable(Level.FINE))
147: LOG
148: .log(
149: Level.FINE,
150: "CommonCellEditor.getCellEditorValue ",
151: e);
152: return origValue;
153: }
154: } else if (textFieldType == TextFieldType.LONG) {
155: if (textField.getText().length() == 0) {
156: return null;
157: }
158:
159: try {
160: return Long.valueOf(textField.getText());
161: } catch (Exception e) {
162: if (LOG.isLoggable(Level.FINE))
163: LOG
164: .log(
165: Level.FINE,
166: "CommonCellEditor.getCellEditorValue ",
167: e);
168: return origValue;
169: }
170: } else if (textFieldType == TextFieldType.DOUBLE) {
171: if (textField.getText().length() == 0) {
172: return null;
173: }
174:
175: try {
176: return Double.valueOf(textField.getText());
177: } catch (Exception e) {
178: if (LOG.isLoggable(Level.FINE))
179: LOG
180: .log(
181: Level.FINE,
182: "CommonCellEditor.getCellEditorValue ",
183: e);
184: return origValue;
185: }
186: } else if (textFieldType == TextFieldType.FLOAT) {
187: if (textField.getText().length() == 0) {
188: return null;
189: }
190:
191: try {
192: return Float.valueOf(textField.getText());
193: } catch (Exception e) {
194: if (LOG.isLoggable(Level.FINE))
195: LOG
196: .log(
197: Level.FINE,
198: "CommonCellEditor.getCellEditorValue ",
199: e);
200: return origValue;
201: }
202: } else if (textFieldType == TextFieldType.URL) {
203: if (textField.getText().length() == 0) {
204: return null;
205: }
206:
207: try {
208: return new URL(textField.getText());
209: } catch (Exception e) {
210: if (LOG.isLoggable(Level.FINE))
211: LOG
212: .log(
213: Level.FINE,
214: "CommonCellEditor.getCellEditorValue ",
215: e);
216: return origValue;
217: }
218: } else {
219: return textField.getText();
220: }
221: }
222: };
223: textField.addActionListener(delegate);
224: }
225:
226: @NotNull
227: public JComponent getEditorComponent() {
228: return editorComponent;
229: }
230:
231: public CommonCellEditor(@NotNull
232: final JCheckBox checkBox) {
233: editorComponent = checkBox;
234: delegate = new EditorDelegate() {
235: public void setValue(@Nullable
236: Object value) {
237: boolean selected = false;
238: if (value instanceof Boolean) {
239: selected = ((Boolean) value).booleanValue();
240: } else if (value instanceof String) {
241: selected = Boolean.valueOf((String) value)
242: .booleanValue();
243: }
244: Border border = null;
245: if (selected) {
246: border = UIManager
247: .getBorder(UIConstants.TABLE_FOCUS_SELECTED_CELL_HIGHLIGHT_BORDER);
248: }
249: if (border == null) {
250: border = UIManager
251: .getBorder(UIConstants.TABLE_FOCUS_CELL_HIGHLIGHT_BORDER);
252: }
253: checkBox.setBorder(border);
254: if (selected) {
255: checkBox.setText(TranslationManager.getInstance()
256: .getTranslation("R",
257: "Property.Boolean.True"));
258: } else {
259: checkBox.setText(TranslationManager.getInstance()
260: .getTranslation("R",
261: "Property.Boolean.False"));
262: }
263: checkBox.setSelected(selected);
264: }
265:
266: @NotNull
267: public Object getCellEditorValue() {
268: return Boolean.valueOf(checkBox.isSelected());
269: }
270: };
271: checkBox.addActionListener(delegate);
272:
273: if (DRAG_FIX) {
274: checkBox.setRequestFocusEnabled(false);
275: }
276: }
277:
278: public CommonCellEditor(@NotNull
279: final JComboBox comboBox) {
280: editorComponent = comboBox;
281: comboBox.putClientProperty(
282: UIConstants.JCOMBO_BOX_IS_TABLE_CELL_EDITOR,
283: Boolean.TRUE);
284: delegate = new EditorDelegate() {
285: public void setValue(@Nullable
286: Object value) {
287: comboBox.setSelectedItem(value);
288: }
289:
290: @NotNull
291: public Object getCellEditorValue() {
292: return comboBox.getSelectedItem();
293: }
294:
295: public boolean shouldSelectCell(@NotNull
296: EventObject anEvent) {
297: if (anEvent instanceof MouseEvent) {
298: MouseEvent e = (MouseEvent) anEvent;
299: return e.getID() != MouseEvent.MOUSE_DRAGGED;
300: }
301: return true;
302: }
303:
304: public boolean stopCellEditing() {
305: if (comboBox.isEditable()) {
306: // Commit edited value.
307: comboBox.actionPerformed(new ActionEvent(
308: CommonCellEditor.this , 0, ""));
309: }
310: return super .stopCellEditing();
311: }
312: };
313: comboBox.addActionListener(delegate);
314: }
315:
316: @NotNull
317: public Component getComponent() {
318: return editorComponent;
319: }
320:
321: public void setClickCountToStart(int count) {
322: clickCountToStart = count;
323: }
324:
325: public int getClickCountToStart() {
326: return clickCountToStart;
327: }
328:
329: @Nullable
330: public Object getCellEditorValue() {
331: return delegate.getCellEditorValue();
332: }
333:
334: public boolean isCellEditable(@Nullable
335: EventObject anEvent) {
336: return delegate.isCellEditable(anEvent);
337: }
338:
339: public boolean shouldSelectCell(@NotNull
340: EventObject anEvent) {
341: return delegate.shouldSelectCell(anEvent);
342: }
343:
344: public boolean stopCellEditing() {
345: return delegate.stopCellEditing();
346: }
347:
348: public void cancelCellEditing() {
349: delegate.cancelCellEditing();
350: }
351:
352: @NotNull
353: @SuppressWarnings({"unchecked"})
354: public Component getTreeCellEditorComponent(@NotNull
355: JTree tree, @Nullable
356: Object value, boolean isSelected, boolean expanded, boolean leaf,
357: int row) {
358: /*String stringValue = */
359: tree.convertValueToText(value, isSelected, expanded, leaf, row,
360: false);
361:
362: if (value instanceof HashSet) {
363: HashSet<Object> hashSet = (HashSet<Object>) value;
364: if (!hashSet.isEmpty()) {
365: delegate.setValue(hashSet.iterator().next());
366: }
367: }
368: return editorComponent;
369: }
370:
371: @NotNull
372: public Component getTableCellEditorComponent(@NotNull
373: JTable table, @Nullable
374: Object value, boolean isSelected, int row, int column) {
375: if (value instanceof HashSet) {
376: HashSet<?> hashSet = (HashSet<?>) value;
377: if (!hashSet.isEmpty()) {
378: delegate.setValue(hashSet.iterator().next());
379: }
380: }
381: return editorComponent;
382: }
383:
384: protected class EditorDelegate implements ActionListener,
385: ItemListener, Serializable {
386: private static final long serialVersionUID = 1L;
387:
388: @Nullable
389: protected Object value;
390:
391: @Nullable
392: public Object getCellEditorValue() {
393: return value;
394: }
395:
396: public void setValue(@Nullable
397: Object value) {
398: this .value = value;
399: }
400:
401: public boolean isCellEditable(@Nullable
402: EventObject anEvent) {
403: if (anEvent instanceof MouseEvent) {
404: return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
405: }
406: return true;
407: }
408:
409: public boolean shouldSelectCell(@NotNull
410: EventObject anEvent) {
411: return true;
412: }
413:
414: @SuppressWarnings({"UnusedDeclaration"})
415: public boolean startCellEditing(@NotNull
416: EventObject anEvent) {
417: return true;
418: }
419:
420: public boolean stopCellEditing() {
421: fireEditingStopped();
422: return true;
423: }
424:
425: public void cancelCellEditing() {
426: fireEditingCanceled();
427: }
428:
429: public void actionPerformed(@NotNull
430: ActionEvent e) {
431: CommonCellEditor.this .stopCellEditing();
432: }
433:
434: public void itemStateChanged(@NotNull
435: ItemEvent e) {
436: CommonCellEditor.this.stopCellEditing();
437: }
438: }
439:
440: }
|