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.wizard.reportgeneratewizard;
015:
016: import com.jgoodies.forms.layout.CellConstraints;
017: import com.jgoodies.forms.layout.FormLayout;
018: import org.jetbrains.annotations.NonNls;
019: import org.jetbrains.annotations.NotNull;
020: import org.jetbrains.annotations.Nullable;
021: import org.pentaho.reportdesigner.crm.report.IconLoader;
022: import org.pentaho.reportdesigner.crm.report.connection.ColumnInfo;
023: import org.pentaho.reportdesigner.crm.report.wizard.AbstractWizardPage;
024: import org.pentaho.reportdesigner.crm.report.wizard.WizardData;
025: import org.pentaho.reportdesigner.lib.client.components.ComponentFactory;
026: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
027: import org.pentaho.reportdesigner.lib.client.util.UncaughtExcpetionsModel;
028:
029: import javax.swing.*;
030: import javax.swing.event.ListSelectionEvent;
031: import javax.swing.event.ListSelectionListener;
032: import javax.swing.table.AbstractTableModel;
033: import javax.swing.table.DefaultTableCellRenderer;
034: import java.awt.*;
035: import java.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.util.ArrayList;
038: import java.util.logging.Level;
039: import java.util.logging.Logger;
040:
041: /**
042: * User: Martin
043: * Date: 20.01.2006
044: * Time: 09:48:17
045: */
046: public class WizardPageVisibleFields extends AbstractWizardPage {
047: @NonNls
048: @NotNull
049: private static final Logger LOG = Logger
050: .getLogger(WizardPageVisibleFields.class.getName());
051:
052: @NotNull
053: private JPanel centerPanel;
054:
055: @NotNull
056: private JTable table;
057:
058: @NotNull
059: private JButton upButton;
060: @NotNull
061: private JButton downButton;
062:
063: @NotNull
064: private ColumnInfoTableModel columnInfoTableModel;
065:
066: public WizardPageVisibleFields() {
067: upButton = ComponentFactory.createButton("R",
068: "WizardPageVisibleFields.ButtonUp");
069: downButton = ComponentFactory.createButton("R",
070: "WizardPageVisibleFields.ButtonDown");
071:
072: @NonNls
073: FormLayout formLayout = new FormLayout(
074: "4dlu, fill:10dlu:grow, 4dlu, default, 4dlu", "4dlu, "
075: + "pref, " + "4dlu, " + "fill:10dlu:grow, "
076: + "4dlu");
077:
078: centerPanel = new JPanel(formLayout);
079: @NonNls
080: CellConstraints cc = new CellConstraints();
081:
082: ArrayList<FieldInfo> fieldInfos = new ArrayList<FieldInfo>();
083: columnInfoTableModel = new ColumnInfoTableModel(fieldInfos);
084: table = new JTable(columnInfoTableModel);
085: table.getColumnModel().getColumn(0).setPreferredWidth(100);
086: table.getColumnModel().getColumn(1).setPreferredWidth(150);
087: table.getColumnModel().getColumn(2).setPreferredWidth(300);
088:
089: table.getSelectionModel().setSelectionMode(
090: ListSelectionModel.SINGLE_INTERVAL_SELECTION);
091: JScrollPane scrollPane = new JScrollPane(table);
092: scrollPane.setPreferredSize(new Dimension(20, 20));
093: centerPanel.add(scrollPane, cc.xywh(2, 2, 1, 3, "fill, fill"));
094:
095: centerPanel.add(upButton, cc.xy(4, 2));
096: centerPanel.add(downButton, cc.xy(4, 4, "center, top"));
097:
098: table.getColumnModel().getColumn(2).setCellRenderer(
099: new DefaultTableCellRenderer() {
100: @NotNull
101: public Component getTableCellRendererComponent(
102: @NotNull
103: JTable table, @Nullable
104: Object value, boolean isSelected,
105: boolean hasFocus, int row, int column) {
106: JLabel label = (JLabel) super
107: .getTableCellRendererComponent(table,
108: value, isSelected, hasFocus,
109: row, column);
110: try {
111: String s = (String) value;
112: try {
113: label.setText(Class.forName(s)
114: .getSimpleName());
115: } catch (ClassNotFoundException e) {
116: if (LOG.isLoggable(Level.FINE))
117: LOG
118: .log(
119: Level.FINE,
120: "WizardPageVisibleFields.getTableCellRendererComponent ",
121: e);
122: label.setText(s);
123: }
124: } catch (Throwable e) {
125: UncaughtExcpetionsModel.getInstance()
126: .addException(e);
127: }
128: return label;
129: }
130: });
131:
132: table.getSelectionModel().addListSelectionListener(
133: new ListSelectionListener() {
134: public void valueChanged(@NotNull
135: ListSelectionEvent e) {
136: if (e.getValueIsAdjusting()) {
137: return;
138: }
139:
140: updateButtonState();
141: }
142: });
143:
144: upButton.addActionListener(new ActionListener() {
145: public void actionPerformed(@NotNull
146: ActionEvent e) {
147: int index = table.getSelectionModel()
148: .getMinSelectionIndex() - 1;
149: int lastIndex = table.getSelectionModel()
150: .getMaxSelectionIndex();
151:
152: FieldInfo fieldInfo = WizardPageVisibleFields.this .columnInfoTableModel.fieldInfos
153: .get(index);
154: WizardPageVisibleFields.this .columnInfoTableModel
155: .removeFieldInfo(index);
156: WizardPageVisibleFields.this .columnInfoTableModel
157: .insertFieldInfo(lastIndex, fieldInfo);
158:
159: updateButtonState();
160: }
161: });
162:
163: downButton.addActionListener(new ActionListener() {
164: public void actionPerformed(@NotNull
165: ActionEvent e) {
166: int index = table.getSelectionModel()
167: .getMaxSelectionIndex() + 1;
168: int first = table.getSelectionModel()
169: .getMinSelectionIndex();
170:
171: FieldInfo fieldInfo = WizardPageVisibleFields.this .columnInfoTableModel.fieldInfos
172: .get(index);
173: WizardPageVisibleFields.this .columnInfoTableModel
174: .removeFieldInfo(index);
175: WizardPageVisibleFields.this .columnInfoTableModel
176: .insertFieldInfo(first, fieldInfo);
177:
178: table.getSelectionModel().setSelectionInterval(
179: first + 1, index);
180: }
181: });
182: }
183:
184: private void updateButtonState() {
185: if (table.getSelectionModel().getMinSelectionIndex() <= 0) {
186: upButton.setEnabled(false);
187: } else {
188: upButton.setEnabled(true);
189: }
190: if (table.getSelectionModel().getMaxSelectionIndex() == -1
191: || table.getSelectionModel().getMaxSelectionIndex() >= this .columnInfoTableModel
192: .getRowCount() - 1) {
193: downButton.setEnabled(false);
194: } else {
195: downButton.setEnabled(true);
196: }
197: }
198:
199: @NotNull
200: public JComponent getCenterPanel() {
201: @Nullable
202: WizardData wizardData = getWizardDialog().getWizardDatas().get(
203: WizardData.AVAILABLE_COLUMN_INFOS);
204:
205: ArrayList<ColumnInfo> availableColumnInfos = null;
206: if (wizardData != null) {
207: //noinspection unchecked
208: availableColumnInfos = (ArrayList<ColumnInfo>) wizardData
209: .getValue();
210: }
211: if (availableColumnInfos == null) {
212: availableColumnInfos = new ArrayList<ColumnInfo>();
213: }
214:
215: ArrayList<ColumnInfo> tempColumnInfos = new ArrayList<ColumnInfo>(
216: availableColumnInfos);
217:
218: //remove all columns in tablemodel, but not in available
219: for (int i = columnInfoTableModel.fieldInfos.size() - 1; i >= 0; i--) {
220: FieldInfo fieldInfo = columnInfoTableModel.fieldInfos
221: .get(i);
222: if (!tempColumnInfos.contains(fieldInfo.columnInfo)) {
223: columnInfoTableModel.removeFieldInfo(i);
224: } else {
225: tempColumnInfos.remove(fieldInfo.columnInfo);
226: }
227: }
228:
229: //add all not already in table model
230: for (ColumnInfo columnInfo : tempColumnInfos) {
231: columnInfoTableModel.addFieldInfo(new FieldInfo(columnInfo,
232: true));
233: }
234:
235: return centerPanel;
236: }
237:
238: @NotNull
239: public String getTitle() {
240: return TranslationManager.getInstance().getTranslation("R",
241: "WizardPageVisibleFields.Title");
242: }
243:
244: @Nullable
245: public ImageIcon getIcon() {
246: return IconLoader.getInstance().getWizardPageGroupIcon();
247: }
248:
249: @NotNull
250: public String getDescription() {
251: return TranslationManager.getInstance().getTranslation("R",
252: "WizardPageVisibleFields.Description");
253: }
254:
255: public void dispose() {
256: }
257:
258: public boolean canNext() {
259: return true;
260: }
261:
262: public boolean canPrevious() {
263: return true;
264: }
265:
266: public boolean canCancel() {
267: return true;
268: }
269:
270: public boolean canFinish() {
271: return true;
272: }
273:
274: @NotNull
275: public WizardData[] getWizardDatas() {
276: return new WizardData[] { new WizardData(
277: WizardData.VISIBLE_COLUMN_INFOS, columnInfoTableModel
278: .getVisibleColumnInfos()) };
279: }
280:
281: private static class FieldInfo {
282: @NotNull
283: public ColumnInfo columnInfo;
284: public boolean visible;
285:
286: private FieldInfo(@NotNull
287: ColumnInfo columnInfo, boolean visible) {
288: this .columnInfo = columnInfo;
289: this .visible = visible;
290: }
291: }
292:
293: private static class ColumnInfoTableModel extends
294: AbstractTableModel {
295: @NotNull
296: private ArrayList<FieldInfo> fieldInfos;
297:
298: private ColumnInfoTableModel(@NotNull
299: ArrayList<FieldInfo> fieldInfos) {
300: //noinspection ConstantConditions
301: if (fieldInfos == null) {
302: throw new IllegalArgumentException(
303: "fieldInfos must not be null");
304: }
305: this .fieldInfos = fieldInfos;
306: }
307:
308: @Nullable
309: public String getColumnName(int column) {
310: if (column == 0) {
311: return TranslationManager.getInstance().getTranslation(
312: "R", "ColumnInfoTable.Visible");
313: } else if (column == 1) {
314: return TranslationManager.getInstance().getTranslation(
315: "R", "ColumnInfoTable.Name");
316: } else if (column == 2) {
317: return TranslationManager.getInstance().getTranslation(
318: "R", "ColumnInfoTable.Type");
319: }
320:
321: return null;
322: }
323:
324: @NotNull
325: public ArrayList<ColumnInfo> getVisibleColumnInfos() {
326: ArrayList<ColumnInfo> visible = new ArrayList<ColumnInfo>();
327: for (FieldInfo fieldInfo : fieldInfos) {
328: if (fieldInfo.visible) {
329: visible.add(fieldInfo.columnInfo);
330: }
331: }
332: return visible;
333: }
334:
335: public int getRowCount() {
336: return fieldInfos.size();
337: }
338:
339: public int getColumnCount() {
340: return 3;
341: }
342:
343: public void addFieldInfo(@NotNull
344: FieldInfo fieldInfo) {
345: fieldInfos.add(fieldInfo);
346: int index = fieldInfos.size() - 1;
347: fireTableRowsInserted(index, index);
348: }
349:
350: public void insertFieldInfo(int index, @NotNull
351: FieldInfo fieldInfo) {
352: fieldInfos.add(index, fieldInfo);
353: fireTableRowsInserted(index, index);
354: }
355:
356: public void removeFieldInfo(int index) {
357: fieldInfos.remove(index);
358: fireTableRowsDeleted(index, index);
359: }
360:
361: @Nullable
362: public Object getValueAt(int rowIndex, int columnIndex) {
363: if (columnIndex == 0) {
364: return Boolean
365: .valueOf(fieldInfos.get(rowIndex).visible);
366: } else if (columnIndex == 1) {
367: return fieldInfos.get(rowIndex).columnInfo
368: .getColumnName();
369: } else if (columnIndex == 2) {
370: return fieldInfos.get(rowIndex).columnInfo
371: .getColumnClass().getSimpleName();
372: }
373: return null;
374: }
375:
376: public void setValueAt(@NotNull
377: Object aValue, int rowIndex, int columnIndex) {
378: if (columnIndex == 0) {
379: fieldInfos.get(rowIndex).visible = !fieldInfos
380: .get(rowIndex).visible;
381: }
382: }
383:
384: @NotNull
385: public Class<?> getColumnClass(int columnIndex) {
386: if (columnIndex == 0) {
387: return Boolean.class;
388: } else {
389: return String.class;
390: }
391: }
392:
393: public boolean isCellEditable(int rowIndex, int columnIndex) {
394: return columnIndex == 0;
395: }
396: }
397: }
|