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: * SortFieldsDialog.java
028: *
029: * Created on 22 aprile 2005, 18.05
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.subdataset;
034:
035: import it.businesslogic.ireport.SortField;
036: import it.businesslogic.ireport.SubDataset;
037: import it.businesslogic.ireport.gui.event.SubDatasetObjectChangedEvent;
038: import it.businesslogic.ireport.util.I18n;
039: import javax.swing.DefaultListModel;
040: import java.util.*;
041:
042: /**
043: *
044: * @author Administrator
045: */
046: public class SortFieldsDialog extends javax.swing.JDialog {
047:
048: private SubDataset subDataset;
049:
050: /**
051: * Creates new form SortFieldsDialog
052: */
053: public SortFieldsDialog(java.awt.Frame parent, boolean modal) {
054: super (parent, modal);
055: initAll();
056: }
057:
058: public SortFieldsDialog(java.awt.Dialog parent, boolean modal) {
059: super (parent, modal);
060: initAll();
061: }
062:
063: public void initAll() {
064: initComponents();
065:
066: jList1.setModel(new DefaultListModel());
067: it.businesslogic.ireport.util.Misc.centerFrame(this );
068:
069: jList1.setCellRenderer(new SortFieldCellRenderer());
070:
071: javax.swing.KeyStroke escape = javax.swing.KeyStroke
072: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
073: false);
074: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
075: public void actionPerformed(java.awt.event.ActionEvent e) {
076: jButtonCloseActionPerformed(e);
077: }
078: };
079:
080: getRootPane().getInputMap(
081: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
082: escape, "ESCAPE");
083: getRootPane().getActionMap().put("ESCAPE", escapeAction);
084:
085: applyI18n();
086: //to make the default button ...
087: this .getRootPane().setDefaultButton(this .jButtonClose);
088: }
089:
090: public void applyI18n() {
091: // Start autogenerated code ----------------------
092: // End autogenerated code ----------------------
093: this .setTitle(I18n.getString("sortFieldsDialog.title",
094: "Sorting options"));
095: jButtonClose.setText(I18n.getString("close", "Close"));
096:
097: jLabelSortBy.setText(I18n.getString(
098: "sortFieldsDialog.labelSortBy", "Sort by..."));
099: jButtonAdd.setText(I18n.getString("sortFieldsDialog.buttonAdd",
100: "Add field"));
101: jButtonAscDesc.setText(I18n.getString(
102: "sortFieldsDialog.buttonAscDesc", "Asc / Desc"));
103: jButtonModify.setText(I18n.getString(
104: "sortFieldsDialog.buttonModify", "Modify field"));
105: jButtonMoveDown.setText(I18n.getString(
106: "sortFieldsDialog.buttonMoveDown", "Move down"));
107: jButtonMoveUp.setText(I18n.getString(
108: "sortFieldsDialog.buttonMoveUp", "Move up"));
109: jButtonRemove.setText(I18n.getString(
110: "sortFieldsDialog.buttonRemove", "Remove field"));
111: }
112:
113: /** This method is called from within the constructor to
114: * initialize the form.
115: * WARNING: Do NOT modify this code. The content of this method is
116: * always regenerated by the Form Editor.
117: */
118: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
119: private void initComponents() {
120: java.awt.GridBagConstraints gridBagConstraints;
121:
122: jLabelSortBy = new javax.swing.JLabel();
123: jScrollPane1 = new javax.swing.JScrollPane();
124: jList1 = new javax.swing.JList();
125: jPanel1 = new javax.swing.JPanel();
126: jButtonAdd = new javax.swing.JButton();
127: jButtonModify = new javax.swing.JButton();
128: jButtonRemove = new javax.swing.JButton();
129: jButtonMoveUp = new javax.swing.JButton();
130: jButtonMoveDown = new javax.swing.JButton();
131: jButtonAscDesc = new javax.swing.JButton();
132: jPanel2 = new javax.swing.JPanel();
133: jButtonClose = new javax.swing.JButton();
134:
135: getContentPane().setLayout(new java.awt.GridBagLayout());
136:
137: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
138: setTitle("Sorting");
139: jLabelSortBy
140: .setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
141: jLabelSortBy.setText("Sort by...");
142: gridBagConstraints = new java.awt.GridBagConstraints();
143: gridBagConstraints.gridx = 0;
144: gridBagConstraints.gridy = 0;
145: gridBagConstraints.gridwidth = 2;
146: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
147: gridBagConstraints.ipadx = 1;
148: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
149: getContentPane().add(jLabelSortBy, gridBagConstraints);
150:
151: jScrollPane1.setPreferredSize(new java.awt.Dimension(359, 260));
152: jList1
153: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
154: public void valueChanged(
155: javax.swing.event.ListSelectionEvent evt) {
156: jList1ValueChanged(evt);
157: }
158: });
159: jList1.addMouseListener(new java.awt.event.MouseAdapter() {
160: public void mouseClicked(java.awt.event.MouseEvent evt) {
161: jList1MouseClicked(evt);
162: }
163: });
164:
165: jScrollPane1.setViewportView(jList1);
166:
167: gridBagConstraints = new java.awt.GridBagConstraints();
168: gridBagConstraints.gridx = 0;
169: gridBagConstraints.gridy = 1;
170: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
171: gridBagConstraints.weightx = 1.0;
172: gridBagConstraints.weighty = 1.0;
173: gridBagConstraints.insets = new java.awt.Insets(0, 4, 4, 4);
174: getContentPane().add(jScrollPane1, gridBagConstraints);
175:
176: jPanel1.setLayout(new java.awt.GridBagLayout());
177:
178: jPanel1.setMinimumSize(new java.awt.Dimension(120, 10));
179: jPanel1.setPreferredSize(new java.awt.Dimension(120, 10));
180: jButtonAdd.setText("Add field");
181: jButtonAdd.setMaximumSize(new java.awt.Dimension(200, 26));
182: jButtonAdd.setMinimumSize(new java.awt.Dimension(90, 26));
183: jButtonAdd.setPreferredSize(new java.awt.Dimension(120, 26));
184: jButtonAdd
185: .addActionListener(new java.awt.event.ActionListener() {
186: public void actionPerformed(
187: java.awt.event.ActionEvent evt) {
188: jButtonAddActionPerformed(evt);
189: }
190: });
191:
192: gridBagConstraints = new java.awt.GridBagConstraints();
193: gridBagConstraints.gridx = 0;
194: gridBagConstraints.gridy = 0;
195: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
196: gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
197: jPanel1.add(jButtonAdd, gridBagConstraints);
198:
199: jButtonModify.setText("Modify field");
200: jButtonModify.setMaximumSize(new java.awt.Dimension(200, 26));
201: jButtonModify.setMinimumSize(new java.awt.Dimension(90, 26));
202: jButtonModify.setPreferredSize(new java.awt.Dimension(120, 26));
203: jButtonModify
204: .addActionListener(new java.awt.event.ActionListener() {
205: public void actionPerformed(
206: java.awt.event.ActionEvent evt) {
207: jButtonModifyActionPerformed(evt);
208: }
209: });
210:
211: gridBagConstraints = new java.awt.GridBagConstraints();
212: gridBagConstraints.gridx = 0;
213: gridBagConstraints.gridy = 1;
214: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
215: gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
216: jPanel1.add(jButtonModify, gridBagConstraints);
217:
218: jButtonRemove.setText("Remove field");
219: jButtonRemove.setMaximumSize(new java.awt.Dimension(200, 26));
220: jButtonRemove.setMinimumSize(new java.awt.Dimension(90, 26));
221: jButtonRemove.setPreferredSize(new java.awt.Dimension(120, 26));
222: jButtonRemove
223: .addActionListener(new java.awt.event.ActionListener() {
224: public void actionPerformed(
225: java.awt.event.ActionEvent evt) {
226: jButtonRemoveActionPerformed(evt);
227: }
228: });
229:
230: gridBagConstraints = new java.awt.GridBagConstraints();
231: gridBagConstraints.gridx = 0;
232: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
233: gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
234: jPanel1.add(jButtonRemove, gridBagConstraints);
235:
236: jButtonMoveUp.setText("Move up");
237: jButtonMoveUp.setMaximumSize(new java.awt.Dimension(200, 26));
238: jButtonMoveUp.setMinimumSize(new java.awt.Dimension(90, 26));
239: jButtonMoveUp.setPreferredSize(new java.awt.Dimension(120, 26));
240: jButtonMoveUp
241: .addActionListener(new java.awt.event.ActionListener() {
242: public void actionPerformed(
243: java.awt.event.ActionEvent evt) {
244: jButtonMoveUpActionPerformed(evt);
245: }
246: });
247:
248: gridBagConstraints = new java.awt.GridBagConstraints();
249: gridBagConstraints.gridx = 0;
250: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
251: gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
252: jPanel1.add(jButtonMoveUp, gridBagConstraints);
253:
254: jButtonMoveDown.setText("Move down");
255: jButtonMoveDown.setMaximumSize(new java.awt.Dimension(200, 26));
256: jButtonMoveDown.setMinimumSize(new java.awt.Dimension(90, 26));
257: jButtonMoveDown
258: .setPreferredSize(new java.awt.Dimension(120, 26));
259: jButtonMoveDown
260: .addActionListener(new java.awt.event.ActionListener() {
261: public void actionPerformed(
262: java.awt.event.ActionEvent evt) {
263: jButtonMoveDownActionPerformed(evt);
264: }
265: });
266:
267: gridBagConstraints = new java.awt.GridBagConstraints();
268: gridBagConstraints.gridx = 0;
269: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
270: gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
271: jPanel1.add(jButtonMoveDown, gridBagConstraints);
272:
273: jButtonAscDesc
274: .setIcon(new javax.swing.ImageIcon(
275: getClass()
276: .getResource(
277: "/it/businesslogic/ireport/icons/datasource/arrow_refresh.png")));
278: jButtonAscDesc.setText("Asc / Desc");
279: jButtonAscDesc.setMaximumSize(new java.awt.Dimension(200, 26));
280: jButtonAscDesc.setMinimumSize(new java.awt.Dimension(90, 26));
281: jButtonAscDesc
282: .setPreferredSize(new java.awt.Dimension(120, 26));
283: jButtonAscDesc
284: .addActionListener(new java.awt.event.ActionListener() {
285: public void actionPerformed(
286: java.awt.event.ActionEvent evt) {
287: jButtonAscDescActionPerformed(evt);
288: }
289: });
290:
291: gridBagConstraints = new java.awt.GridBagConstraints();
292: gridBagConstraints.gridx = 0;
293: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
294: gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
295: jPanel1.add(jButtonAscDesc, gridBagConstraints);
296:
297: gridBagConstraints = new java.awt.GridBagConstraints();
298: gridBagConstraints.gridx = 0;
299: gridBagConstraints.weighty = 1.0;
300: jPanel1.add(jPanel2, gridBagConstraints);
301:
302: jButtonClose.setText("Close");
303: jButtonClose.setMaximumSize(new java.awt.Dimension(200, 26));
304: jButtonClose.setMinimumSize(new java.awt.Dimension(90, 26));
305: jButtonClose.setPreferredSize(new java.awt.Dimension(120, 26));
306: jButtonClose
307: .addActionListener(new java.awt.event.ActionListener() {
308: public void actionPerformed(
309: java.awt.event.ActionEvent evt) {
310: jButtonCloseActionPerformed(evt);
311: }
312: });
313:
314: gridBagConstraints = new java.awt.GridBagConstraints();
315: gridBagConstraints.gridx = 0;
316: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
317: jPanel1.add(jButtonClose, gridBagConstraints);
318:
319: gridBagConstraints = new java.awt.GridBagConstraints();
320: gridBagConstraints.gridx = 1;
321: gridBagConstraints.gridy = 1;
322: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
323: gridBagConstraints.weighty = 1.0;
324: gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 4);
325: getContentPane().add(jPanel1, gridBagConstraints);
326:
327: pack();
328: }// </editor-fold>//GEN-END:initComponents
329:
330: private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
331:
332: if (evt.getClickCount() == 2 && evt.getButton() == evt.BUTTON1) {
333: jButtonModifyActionPerformed(null);
334: }
335: }//GEN-LAST:event_jList1MouseClicked
336:
337: private void jButtonAscDescActionPerformed(
338: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAscDescActionPerformed
339:
340: // get selected sortfield...
341: int[] items = jList1.getSelectedIndices();
342: for (int i = 0; i < items.length; ++i) {
343: SortField field = (SortField) jList1.getModel()
344: .getElementAt(items[i]);
345: field.setDesc(!field.isDesc());
346: getSubDataset()
347: .fireSubDatasetObjectChangedListenerSubDatasetObjectChanged(
348: new SubDatasetObjectChangedEvent(
349: getSubDataset(),
350: SubDatasetObjectChangedEvent.SORTFIELD,
351: SubDatasetObjectChangedEvent.MODIFIED,
352: field, field));
353: }
354:
355: jList1.updateUI();
356:
357: }//GEN-LAST:event_jButtonAscDescActionPerformed
358:
359: private void jList1ValueChanged(
360: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
361:
362: if (jList1.getSelectedIndex() >= 0) {
363: jButtonAscDesc.setEnabled(true);
364: jButtonModify.setEnabled(true);
365: jButtonRemove.setEnabled(true);
366: jButtonMoveUp.setEnabled(jList1.getSelectedIndex() > 0);
367: jButtonMoveDown
368: .setEnabled(jList1.getSelectedIndex() < ((DefaultListModel) jList1
369: .getModel()).size() - 1);
370: } else {
371: jButtonAscDesc.setEnabled(false);
372: jButtonModify.setEnabled(false);
373: jButtonRemove.setEnabled(false);
374: jButtonMoveUp.setEnabled(false);
375: jButtonMoveDown.setEnabled(false);
376: }
377: }//GEN-LAST:event_jList1ValueChanged
378:
379: private void jButtonModifyActionPerformed(
380: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonModifyActionPerformed
381:
382: // get selected sortfield...
383: if (jList1.getSelectedIndex() >= 0) {
384: SortField field = (SortField) jList1.getSelectedValue();
385: SortFieldDialog sfd = new SortFieldDialog(this , true);
386: sfd.setSubDataset(getSubDataset());
387: sfd.setSortField(field);
388:
389: sfd.setVisible(true);
390:
391: if (sfd.getDialogResult() == javax.swing.JOptionPane.OK_OPTION) {
392: field.setFieldName(sfd.getSortField().getFieldName());
393: field.setDesc(sfd.getSortField().isDesc());
394:
395: getSubDataset()
396: .fireSubDatasetObjectChangedListenerSubDatasetObjectChanged(
397: new SubDatasetObjectChangedEvent(
398: getSubDataset(),
399: SubDatasetObjectChangedEvent.SORTFIELD,
400: SubDatasetObjectChangedEvent.MODIFIED,
401: field, field));
402:
403: jList1.updateUI();
404: }
405: }
406:
407: }//GEN-LAST:event_jButtonModifyActionPerformed
408:
409: private void jButtonCloseActionPerformed(
410: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
411: setVisible(false);
412: }//GEN-LAST:event_jButtonCloseActionPerformed
413:
414: private void jButtonMoveUpActionPerformed(
415: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMoveUpActionPerformed
416:
417: if (jList1.getSelectedValues() != null) {
418: int[] indices = jList1.getSelectedIndices();
419: for (int i = 0; i < indices.length; ++i) {
420: if (indices[i] == 0)
421: continue;
422: Object val = ((DefaultListModel) jList1.getModel())
423: .remove(indices[i]);
424: ((DefaultListModel) jList1.getModel()).insertElementAt(
425: val, indices[i] - 1);
426: indices[i]--;
427: }
428: jList1.setSelectedIndices(indices);
429: // update the list...
430: getSubDataset().getSortFields().removeAllElements();
431: DefaultListModel dlm = (DefaultListModel) jList1.getModel();
432: for (int i = 0; i < dlm.getSize(); ++i) {
433: getSubDataset().getSortFields().add(dlm.get(i));
434: }
435: getSubDataset()
436: .fireSubDatasetObjectChangedListenerSubDatasetObjectChanged(
437: new SubDatasetObjectChangedEvent(
438: getSubDataset(),
439: SubDatasetObjectChangedEvent.SORTFIELD,
440: SubDatasetObjectChangedEvent.MODIFIED,
441: null, null));
442: }
443:
444: }//GEN-LAST:event_jButtonMoveUpActionPerformed
445:
446: private void jButtonMoveDownActionPerformed(
447: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMoveDownActionPerformed
448: if (jList1.getSelectedValues() != null) {
449: int[] indices = jList1.getSelectedIndices();
450: for (int i = indices.length - 1; i >= 0; --i) {
451: if (indices[i] >= ((DefaultListModel) jList1.getModel())
452: .size() - 1)
453: continue;
454:
455: Object val = ((DefaultListModel) jList1.getModel())
456: .remove(indices[i]);
457: ((DefaultListModel) jList1.getModel()).insertElementAt(
458: val, indices[i] + 1);
459: indices[i]++;
460: }
461: jList1.setSelectedIndices(indices);
462:
463: getSubDataset().getSortFields().removeAllElements();
464: DefaultListModel dlm = (DefaultListModel) jList1.getModel();
465: for (int i = 0; i < dlm.getSize(); ++i) {
466: getSubDataset().getSortFields().add(dlm.get(i));
467: }
468:
469: getSubDataset()
470: .fireSubDatasetObjectChangedListenerSubDatasetObjectChanged(
471: new SubDatasetObjectChangedEvent(
472: getSubDataset(),
473: SubDatasetObjectChangedEvent.SORTFIELD,
474: SubDatasetObjectChangedEvent.MODIFIED,
475: null, null));
476: }
477: }//GEN-LAST:event_jButtonMoveDownActionPerformed
478:
479: private void jButtonRemoveActionPerformed(
480: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveActionPerformed
481:
482: if (jList1.getSelectedValues() != null) {
483: Object[] values = jList1.getSelectedValues();
484: for (int i = 0; i < values.length; ++i) {
485: SortField field = (SortField) values[i];
486: ((DefaultListModel) jList1.getModel())
487: .removeElement(field);
488: getSubDataset().removeSortField(field);
489: }
490: }
491:
492: }//GEN-LAST:event_jButtonRemoveActionPerformed
493:
494: private void jButtonAddActionPerformed(
495: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
496:
497: SortFieldDialog jrpd = new SortFieldDialog(this , true);
498: jrpd.setSubDataset(getSubDataset());
499: jrpd.setVisible(true);
500:
501: if (jrpd.getDialogResult() == javax.swing.JOptionPane.OK_OPTION) {
502:
503: SortField field = jrpd.getSortField();
504: ((DefaultListModel) jList1.getModel()).addElement(field);
505: getSubDataset().addSortField(field);
506: }
507:
508: }//GEN-LAST:event_jButtonAddActionPerformed
509:
510: //getSubDataset().fireSubDatasetObjectChangedListenerSubDatasetObjectChanged( new SubDatasetObjectChangedEvent( getSubDataset() ,SubDatasetObjectChangedEvent.SORTFIELD, SubDatasetObjectChangedEvent.MODIFIED, null, null));
511:
512: public SubDataset getSubDataset() {
513: return subDataset;
514: }
515:
516: public void setSubDataset(SubDataset subDataset) {
517: this .subDataset = subDataset;
518: Vector cp = subDataset.getSortFields();
519: for (int i = 0; i < cp.size(); ++i) {
520: ((DefaultListModel) jList1.getModel())
521: .addElement((SortField) cp.elementAt(i));
522: }
523: }
524:
525: // Variables declaration - do not modify//GEN-BEGIN:variables
526: private javax.swing.JButton jButtonAdd;
527: private javax.swing.JButton jButtonAscDesc;
528: private javax.swing.JButton jButtonClose;
529: private javax.swing.JButton jButtonModify;
530: private javax.swing.JButton jButtonMoveDown;
531: private javax.swing.JButton jButtonMoveUp;
532: private javax.swing.JButton jButtonRemove;
533: private javax.swing.JLabel jLabelSortBy;
534: private javax.swing.JList jList1;
535: private javax.swing.JPanel jPanel1;
536: private javax.swing.JPanel jPanel2;
537: private javax.swing.JScrollPane jScrollPane1;
538: // End of variables declaration//GEN-END:variables
539:
540: }
|