001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * StylesDialog.java
028: *
029: * Created on 7 maggio 2003, 23.43
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.style;
034:
035: import javax.swing.table.*;
036: import javax.swing.*;
037: import javax.swing.event.*;
038: import it.businesslogic.ireport.*;
039: import it.businesslogic.ireport.gui.MainFrame;
040: import it.businesslogic.ireport.gui.event.StyleChangedEvent;
041: import it.businesslogic.ireport.util.Misc;
042:
043: import java.util.*;
044: import it.businesslogic.ireport.util.I18n;
045:
046: /**
047: *
048: * @author Administrator
049: */
050: public class StylesDialog extends javax.swing.JDialog implements
051: it.businesslogic.ireport.gui.event.StyleChangedListener {
052:
053: /** Creates new form ValuesDialog */
054: public StylesDialog(java.awt.Frame parent, boolean modal) {
055: super (parent, modal);
056: initComponents();
057: applyI18n();
058: this .setSize(490, 250);
059: //DefaultTableColumnModel dtcm = (DefaultTableColumnModel)jTableParameters.getColumnModel();
060: //DefaultTableCellRenderer tcr = (DefaultTableCellRenderer)dtcm.getColumn(0).getHeaderRenderer();
061:
062: //new DefaultTableCellRenderer();
063: //tcr.setFont(jTableParameters.getFont());
064: //tcr.setBackground(this.getBackground());
065: //tcr.setBorder( new javax.swing.border.BevelBorder( javax.swing.border.BevelBorder.RAISED));
066: //dtcm.getColumn(0).setHeaderRenderer(tcr);
067:
068: DefaultListSelectionModel dlsm = (DefaultListSelectionModel) this .jTableParameters
069: .getSelectionModel();
070: dlsm
071: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
072: public void valueChanged(ListSelectionEvent e) {
073: jTableParametersListSelectionValueChanged(e);
074: }
075: });
076:
077: // Open in center...
078: it.businesslogic.ireport.util.Misc.centerFrame(this );
079:
080: DefaultTableModel dtm = (DefaultTableModel) jTableParameters
081: .getModel();
082:
083: it.businesslogic.ireport.gui.MainFrame.getMainInstance()
084: .getActiveReportFrame().getReport()
085: .addStyleChangedListener(this );
086: Enumeration e = it.businesslogic.ireport.gui.MainFrame
087: .getMainInstance().getActiveReportFrame().getReport()
088: .getStyles().elements();
089: while (e.hasMoreElements()) {
090: Style style = (Style) e.nextElement();
091: addStyle(style);
092: }
093:
094: DefaultTableColumnModel dtcm = (DefaultTableColumnModel) jTableParameters
095: .getColumnModel();
096: dtcm.getColumn(0).setPreferredWidth(350);
097: dtcm.getColumn(1).setPreferredWidth(100);
098:
099: javax.swing.KeyStroke escape = javax.swing.KeyStroke
100: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
101: false);
102: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
103: public void actionPerformed(java.awt.event.ActionEvent e) {
104: setVisible(false);
105: }
106: };
107:
108: getRootPane().getInputMap(
109: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
110: escape, "ESCAPE");
111: getRootPane().getActionMap().put("ESCAPE", escapeAction);
112:
113: }
114:
115: public void jTableParametersListSelectionValueChanged(
116: javax.swing.event.ListSelectionEvent e) {
117: if (this .jTableParameters.getSelectedRowCount() > 0) {
118: this .jButtonModify.setEnabled(true);
119: this .jButtonDelete.setEnabled(true);
120: this .jButtonDefault.setEnabled(true);
121: } else {
122: this .jButtonModify.setEnabled(false);
123: this .jButtonDelete.setEnabled(false);
124: this .jButtonDefault.setEnabled(false);
125: }
126: }
127:
128: /** This method is called from within the constructor to
129: * initialize the form.
130: * WARNING: Do NOT modify this code. The content of this method is
131: * always regenerated by the Form Editor.
132: */
133: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
134: private void initComponents() {
135: java.awt.GridBagConstraints gridBagConstraints;
136:
137: jPanelParameters = new javax.swing.JPanel();
138: jScrollPane1 = new javax.swing.JScrollPane();
139: jTableParameters = new javax.swing.JTable();
140: jPanelButtons = new javax.swing.JPanel();
141: jButtonNew = new javax.swing.JButton();
142: jButtonModify = new javax.swing.JButton();
143: jButtonDefault = new javax.swing.JButton();
144: jButtonDelete = new javax.swing.JButton();
145:
146: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
147: setTitle("Styles");
148: addWindowListener(new java.awt.event.WindowAdapter() {
149: public void windowClosing(java.awt.event.WindowEvent evt) {
150: closeDialog(evt);
151: }
152: });
153:
154: jPanelParameters.setLayout(new java.awt.BorderLayout(2, 2));
155:
156: jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
157: jTableParameters
158: .setModel(new javax.swing.table.DefaultTableModel(
159: new Object[][] {
160:
161: }, new String[] { "Style name", "Default" }) {
162: boolean[] canEdit = new boolean[] { false, false };
163:
164: public boolean isCellEditable(int rowIndex,
165: int columnIndex) {
166: return canEdit[columnIndex];
167: }
168: });
169: jTableParameters
170: .addMouseListener(new java.awt.event.MouseAdapter() {
171: public void mouseClicked(
172: java.awt.event.MouseEvent evt) {
173: jTableParametersMouseClicked(evt);
174: }
175: });
176:
177: jScrollPane1.setViewportView(jTableParameters);
178:
179: jPanelParameters
180: .add(jScrollPane1, java.awt.BorderLayout.CENTER);
181:
182: jPanelButtons.setLayout(new java.awt.GridBagLayout());
183:
184: jPanelButtons.setMinimumSize(new java.awt.Dimension(100, 10));
185: jPanelButtons.setPreferredSize(new java.awt.Dimension(100, 10));
186: jButtonNew.setText("New");
187: jButtonNew.setMargin(new java.awt.Insets(2, 4, 2, 4));
188: jButtonNew
189: .addActionListener(new java.awt.event.ActionListener() {
190: public void actionPerformed(
191: java.awt.event.ActionEvent evt) {
192: jButtonNewActionPerformed(evt);
193: }
194: });
195:
196: gridBagConstraints = new java.awt.GridBagConstraints();
197: gridBagConstraints.gridx = 0;
198: gridBagConstraints.gridy = 0;
199: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
200: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
201: gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
202: jPanelButtons.add(jButtonNew, gridBagConstraints);
203:
204: jButtonModify.setText("Modify");
205: jButtonModify.setEnabled(false);
206: jButtonModify.setMargin(new java.awt.Insets(2, 4, 2, 4));
207: jButtonModify
208: .addActionListener(new java.awt.event.ActionListener() {
209: public void actionPerformed(
210: java.awt.event.ActionEvent evt) {
211: jButtonModifyActionPerformed(evt);
212: }
213: });
214:
215: gridBagConstraints = new java.awt.GridBagConstraints();
216: gridBagConstraints.gridx = 0;
217: gridBagConstraints.gridy = 1;
218: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
219: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
220: gridBagConstraints.insets = new java.awt.Insets(0, 3, 5, 3);
221: jPanelButtons.add(jButtonModify, gridBagConstraints);
222:
223: jButtonDefault.setText("Set as default");
224: jButtonDefault.setEnabled(false);
225: jButtonDefault.setMargin(new java.awt.Insets(2, 4, 2, 4));
226: jButtonDefault
227: .addActionListener(new java.awt.event.ActionListener() {
228: public void actionPerformed(
229: java.awt.event.ActionEvent evt) {
230: jButtonDefaultActionPerformed(evt);
231: }
232: });
233:
234: gridBagConstraints = new java.awt.GridBagConstraints();
235: gridBagConstraints.gridx = 0;
236: gridBagConstraints.gridy = 2;
237: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
238: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
239: gridBagConstraints.weightx = 1.0;
240: gridBagConstraints.insets = new java.awt.Insets(0, 3, 5, 3);
241: jPanelButtons.add(jButtonDefault, gridBagConstraints);
242:
243: jButtonDelete.setText("Delete");
244: jButtonDelete.setEnabled(false);
245: jButtonDelete.setMargin(new java.awt.Insets(2, 4, 2, 4));
246: jButtonDelete
247: .addActionListener(new java.awt.event.ActionListener() {
248: public void actionPerformed(
249: java.awt.event.ActionEvent evt) {
250: jButtonDeleteActionPerformed(evt);
251: }
252: });
253:
254: gridBagConstraints = new java.awt.GridBagConstraints();
255: gridBagConstraints.gridx = 0;
256: gridBagConstraints.gridy = 3;
257: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
258: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
259: gridBagConstraints.weightx = 1.0;
260: gridBagConstraints.weighty = 1.0;
261: gridBagConstraints.insets = new java.awt.Insets(0, 3, 5, 3);
262: jPanelButtons.add(jButtonDelete, gridBagConstraints);
263:
264: jPanelParameters.add(jPanelButtons, java.awt.BorderLayout.EAST);
265:
266: getContentPane().add(jPanelParameters,
267: java.awt.BorderLayout.CENTER);
268:
269: pack();
270: }// </editor-fold>//GEN-END:initComponents
271:
272: private void jButtonDefaultActionPerformed(
273: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDefaultActionPerformed
274:
275: if (jTableParameters.getSelectedRow() < 0)
276: return;
277: Style irc = null;
278: try {
279: irc = (Style) jTableParameters.getModel().getValueAt(
280: jTableParameters.getSelectedRow(), 0);
281: } catch (Exception ex) {
282: return;
283: }
284:
285: if (irc == null)
286: return;
287:
288: irc.getAttributes().put(irc.ATTRIBUTE_isDefault, "true");
289: jTableParameters.setValueAt("Yes", jTableParameters
290: .getSelectedRow(), 1);
291:
292: for (int i = 0; i < jTableParameters.getRowCount(); ++i) {
293: if (i != jTableParameters.getSelectedRow()) {
294: Style s = (Style) jTableParameters.getModel()
295: .getValueAt(i, 0);
296: if (s != null)
297: s.getAttributes().remove(s.ATTRIBUTE_isDefault);
298: jTableParameters.setValueAt("", i, 1);
299: }
300: }
301:
302: MainFrame.getMainInstance().getActiveReportFrame().getReport()
303: .fireStyleChangedListenerStyleChanged(
304: new StyleChangedEvent(MainFrame
305: .getMainInstance()
306: .getActiveReportFrame().getReport(),
307: StyleChangedEvent.CHANGED, irc, irc));
308: }//GEN-LAST:event_jButtonDefaultActionPerformed
309:
310: private void jTableParametersMouseClicked(
311: java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableParametersMouseClicked
312: if (evt.getClickCount() == 2 && evt.getButton() == evt.BUTTON1) {
313: jButtonModifyActionPerformed(null);
314: }
315: }//GEN-LAST:event_jTableParametersMouseClicked
316:
317: private void jButtonDeleteActionPerformed(
318: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteActionPerformed
319: // Get the selected connection...
320: if (jTableParameters.getSelectedRowCount() > 0) {
321: Style irc = null;
322: try {
323: irc = (Style) jTableParameters.getModel().getValueAt(
324: jTableParameters.getSelectedRow(), 0);
325: } catch (Exception ex) {
326: return;
327: }
328:
329: if (javax.swing.JOptionPane.showConfirmDialog(this , I18n
330: .getString("messages.stylesDialog.removingStyle",
331: "Do you want really remove this style?"),
332: "", javax.swing.JOptionPane.YES_NO_CANCEL_OPTION,
333: javax.swing.JOptionPane.QUESTION_MESSAGE) == javax.swing.JOptionPane.YES_OPTION) {
334: MainFrame.getMainInstance().getActiveReportFrame()
335: .getReport().removeStyle(irc);
336:
337: ((DefaultTableModel) jTableParameters.getModel())
338: .removeRow(jTableParameters.getSelectedRow());
339: jTableParameters.updateUI();
340: }
341: }
342:
343: }//GEN-LAST:event_jButtonDeleteActionPerformed
344:
345: private void jButtonModifyActionPerformed(
346: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonModifyActionPerformed
347: java.awt.Frame parent = Misc.frameFromComponent(this );
348: StyleDialog cd = new StyleDialog(this , true);
349:
350: // Take the selected connection from the table...
351: if (jTableParameters.getSelectedRow() < 0)
352: return;
353: Style irc = null;
354: try {
355: irc = (Style) jTableParameters.getModel().getValueAt(
356: jTableParameters.getSelectedRow(), 0);
357: } catch (Exception ex) {
358: return;
359: }
360:
361: if (irc == null)
362: return;
363:
364: cd.setStyle(irc);
365: cd.setVisible(true);
366:
367: if (cd.getDialogResult() == JOptionPane.OK_OPTION) {
368: Style sub = cd.getStyle();
369: // Now we have an old and a new object.
370: // 1. Adjust table...
371: try {
372: jTableParameters.getModel().setValueAt(sub,
373: jTableParameters.getSelectedRow(), 0);
374: } catch (Exception ex) {
375: return;
376: }
377: }
378: }//GEN-LAST:event_jButtonModifyActionPerformed
379:
380: private void jButtonNewActionPerformed(
381: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewActionPerformed
382: java.awt.Frame parent = Misc.frameFromComponent(this );
383: StyleDialog cd = new StyleDialog(this , true);
384: cd.setVisible(true);
385: }//GEN-LAST:event_jButtonNewActionPerformed
386:
387: public void addStyle(Style s) {
388: DefaultTableModel dtm = (DefaultTableModel) jTableParameters
389: .getModel();
390: dtm.addRow(new Object[] {
391: s,
392: (s
393: .getAttributeBoolean(Style.ATTRIBUTE_isDefault,
394: false) ? "Yes" : "") });
395: }
396:
397: public void templateChanged(
398: it.businesslogic.ireport.gui.event.TemplateChangedEvent evt) {
399:
400: }
401:
402: public void styleChanged(
403: it.businesslogic.ireport.gui.event.StyleChangedEvent evt) {
404:
405: if (evt.getAction() == evt.ADDED) {
406: this .addStyle(evt.getNewValue());
407: } else {
408: for (int i = 0; i < jTableParameters.getRowCount(); ++i) {
409: if (jTableParameters.getModel().getValueAt(i, 0) == evt
410: .getOldValue()) {
411: if (evt.getAction() == evt.CHANGED) {
412: jTableParameters.getModel().setValueAt(
413: evt.getNewValue(), i, 0);
414: jTableParameters.getModel().setValueAt(
415: (evt.getNewValue().getAttributeBoolean(
416: Style.ATTRIBUTE_isDefault,
417: false) ? "Yes" : ""), i, 1);
418: } else {
419: ((javax.swing.table.DefaultTableModel) jTableParameters
420: .getModel()).removeRow(i);
421: }
422: jTableParameters.updateUI();
423: break;
424: }
425: }
426: }
427:
428: for (int i = 0; i < jTableParameters.getRowCount(); ++i) {
429: Style s = (Style) jTableParameters.getModel().getValueAt(i,
430: 0);
431: jTableParameters.setValueAt((s.getAttributeBoolean(
432: Style.ATTRIBUTE_isDefault, false) ? "Yes" : ""), i,
433: 1);
434: }
435:
436: }
437:
438: /** Closes the dialog */
439: private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
440: it.businesslogic.ireport.gui.MainFrame.getMainInstance()
441: .getActiveReportFrame().getReport()
442: .removeStyleChangedListener(this );
443: setVisible(false);
444: }//GEN-LAST:event_closeDialog
445:
446: // Variables declaration - do not modify//GEN-BEGIN:variables
447: private javax.swing.JButton jButtonDefault;
448: private javax.swing.JButton jButtonDelete;
449: private javax.swing.JButton jButtonModify;
450: private javax.swing.JButton jButtonNew;
451: private javax.swing.JPanel jPanelButtons;
452: private javax.swing.JPanel jPanelParameters;
453: private javax.swing.JScrollPane jScrollPane1;
454: private javax.swing.JTable jTableParameters;
455:
456: // End of variables declaration//GEN-END:variables
457:
458: public void applyI18n() {
459: // Start autogenerated code ----------------------
460: jButtonDefault.setText(I18n.getString(
461: "stylesDialog.buttonDefault", "Set as default"));
462: jButtonDelete.setText(I18n.getString(
463: "stylesDialog.buttonDelete", "Delete"));
464: jButtonModify.setText(I18n.getString(
465: "stylesDialog.buttonModify", "Modify"));
466: jButtonNew.setText(I18n.getString("stylesDialog.buttonNew",
467: "New"));
468: // End autogenerated code ----------------------
469:
470: jTableParameters.getColumnModel().getColumn(0).setHeaderValue(
471: I18n.getString("stylesDialog.tablecolumn.styleName",
472: "Style name"));
473: jTableParameters.getColumnModel().getColumn(1).setHeaderValue(
474: I18n.getString("stylesDialog.tablecolumn.default",
475: "Default"));
476:
477: //
478: this .setTitle(I18n.getString("stylesDialog.title", "Styles"));
479: //
480: }
481: }
|