KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > style > StylesView


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * StylesView.java
28  *
29  * Created on March 20, 2006, 5:15 PM
30  *
31  */

32
33 package it.businesslogic.ireport.gui.style;
34
35 import it.businesslogic.ireport.Report;
36 import it.businesslogic.ireport.Style;
37 import it.businesslogic.ireport.gui.JReportFrame;
38 import it.businesslogic.ireport.gui.MainFrame;
39 import it.businesslogic.ireport.gui.dnd.JListTransfertHandler;
40 import it.businesslogic.ireport.gui.docking.GenericDragTargetListener;
41 import it.businesslogic.ireport.gui.event.ReportFrameActivatedEvent;
42 import it.businesslogic.ireport.gui.event.ReportFrameActivatedListener;
43 import it.businesslogic.ireport.gui.event.StyleChangedEvent;
44 import it.businesslogic.ireport.gui.event.StyleChangedListener;
45 import it.businesslogic.ireport.gui.sheet.Tag;
46 import it.businesslogic.ireport.util.I18n;
47 import it.businesslogic.ireport.util.LanguageChangedEvent;
48 import it.businesslogic.ireport.util.LanguageChangedListener;
49 import java.awt.dnd.DnDConstants JavaDoc;
50 import java.awt.dnd.DropTarget JavaDoc;
51 import java.awt.dnd.DropTargetListener JavaDoc;
52 import java.util.Vector JavaDoc;
53 import javax.swing.DefaultListModel JavaDoc;
54 import javax.swing.JOptionPane JavaDoc;
55
56 /**
57  *
58  * @author gtoffoli
59  */

60 public class StylesView extends javax.swing.JPanel JavaDoc implements LanguageChangedListener, ReportFrameActivatedListener, StyleChangedListener
61 {
62     private JReportFrame activeReportFrame = null;
63     private DefaultListModel JavaDoc listModel = null;
64     
65     /** Creates new form StylesView */
66     public StylesView() {
67         initComponents();
68         applyI18n();
69         listModel = new DefaultListModel JavaDoc();
70         jListStyles.setModel( listModel );
71         jListStyles.setDropTarget(new DropTarget JavaDoc(this, DnDConstants.ACTION_MOVE, new GenericDragTargetListener(), true));
72         
73         jComboBoxStyleType.addItem(new Tag("reportStyles", it.businesslogic.ireport.util.I18n.getString("stylesView.reportStyles","Report styles")));
74         jComboBoxStyleType.addItem(new Tag("libraryStyles", it.businesslogic.ireport.util.I18n.getString("stylesView.stylesLibrary","Styles library")));
75         
76         jListStyles.setTransferHandler(new JListTransfertHandler());
77         
78        // System.out.println(this.getTransferHandler());
79

80         languageChanged(null);
81         
82         MainFrame.getMainInstance().addReportFrameActivatedListener( this );
83         
84         
85         updateStyles();
86     }
87     
88     /** This method is called from within the constructor to
89      * initialize the form.
90      * WARNING: Do NOT modify this code. The content of this method is
91      * always regenerated by the Form Editor.
92      */

93     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
94
private void initComponents() {
95         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
96
97         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
98         jListStyles = new it.businesslogic.ireport.gui.style.JBGList();
99         jPanel1 = new javax.swing.JPanel JavaDoc();
100         jComboBoxStyleType = new javax.swing.JComboBox JavaDoc();
101         jToolBar1 = new javax.swing.JToolBar JavaDoc();
102         jButtonAddStyle = new javax.swing.JButton JavaDoc();
103         jButtonEditStyle = new javax.swing.JButton JavaDoc();
104         jButtonDeleteStyle = new javax.swing.JButton JavaDoc();
105
106         setLayout(new java.awt.BorderLayout JavaDoc());
107
108         jScrollPane1.setBackground(new java.awt.Color JavaDoc(255, 255, 255));
109         jListStyles.setModel(new javax.swing.AbstractListModel JavaDoc() {
110             String JavaDoc[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
111             public int getSize() { return strings.length; }
112             public Object JavaDoc getElementAt(int i) { return strings[i]; }
113         });
114         jListStyles.setDragEnabled(true);
115         jListStyles.setOpaque(false);
116         jListStyles.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
117             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
118                 jListStylesValueChanged(evt);
119             }
120         });
121         jListStyles.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
122             public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
123                 jListStylesMouseClicked(evt);
124             }
125         });
126
127         jScrollPane1.setViewportView(jListStyles);
128
129         add(jScrollPane1, java.awt.BorderLayout.CENTER);
130
131         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
132
133         jComboBoxStyleType.addActionListener(new java.awt.event.ActionListener JavaDoc() {
134             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
135                 jComboBoxStyleTypeActionPerformed(evt);
136             }
137         });
138
139         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
140         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
141         gridBagConstraints.weightx = 1.0;
142         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 4);
143         jPanel1.add(jComboBoxStyleType, gridBagConstraints);
144
145         jToolBar1.setFloatable(false);
146         jButtonAddStyle.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/it/businesslogic/ireport/icons/styles/style_add.png")));
147         jButtonAddStyle.setToolTipText("Style add");
148         jButtonAddStyle.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
149         jButtonAddStyle.setFocusPainted(false);
150         jButtonAddStyle.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
151         jButtonAddStyle.addActionListener(new java.awt.event.ActionListener JavaDoc() {
152             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
153                 jButtonAddStyleActionPerformed(evt);
154             }
155         });
156
157         jToolBar1.add(jButtonAddStyle);
158
159         jButtonEditStyle.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/it/businesslogic/ireport/icons/styles/style_edit.png")));
160         jButtonEditStyle.setToolTipText("Style add");
161         jButtonEditStyle.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
162         jButtonEditStyle.setFocusPainted(false);
163         jButtonEditStyle.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
164         jButtonEditStyle.addActionListener(new java.awt.event.ActionListener JavaDoc() {
165             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
166                 jButtonEditStyleActionPerformed(evt);
167             }
168         });
169
170         jToolBar1.add(jButtonEditStyle);
171
172         jButtonDeleteStyle.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/it/businesslogic/ireport/icons/styles/style_delete.png")));
173         jButtonDeleteStyle.setToolTipText("Style add");
174         jButtonDeleteStyle.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
175         jButtonDeleteStyle.setFocusPainted(false);
176         jButtonDeleteStyle.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
177         jButtonDeleteStyle.addActionListener(new java.awt.event.ActionListener JavaDoc() {
178             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
179                 jButtonDeleteStyleActionPerformed(evt);
180             }
181         });
182
183         jToolBar1.add(jButtonDeleteStyle);
184
185         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
186         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
187         gridBagConstraints.weighty = 1.0;
188         jPanel1.add(jToolBar1, gridBagConstraints);
189
190         add(jPanel1, java.awt.BorderLayout.NORTH);
191
192     }// </editor-fold>//GEN-END:initComponents
193

194     private void jComboBoxStyleTypeActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jComboBoxStyleTypeActionPerformed
195

196         updateStyles();
197         
198     }//GEN-LAST:event_jComboBoxStyleTypeActionPerformed
199

200     private void jListStylesMouseClicked(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_jListStylesMouseClicked
201

202         if (evt.getClickCount() == 2 && evt.getButton() == evt.BUTTON1)
203         {
204             jButtonEditStyleActionPerformed(null);
205         }
206         
207     }//GEN-LAST:event_jListStylesMouseClicked
208

209     private void jListStylesValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_jListStylesValueChanged
210

211         
212         if (jListStyles.getSelectedIndex() >= 0)
213         {
214             jButtonEditStyle.setEnabled(true);
215             jButtonDeleteStyle.setEnabled(true);
216         }
217         else
218         {
219             jButtonEditStyle.setEnabled(false);
220             jButtonDeleteStyle.setEnabled(false);
221         }
222         
223         
224     }//GEN-LAST:event_jListStylesValueChanged
225

226     private void jButtonDeleteStyleActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonDeleteStyleActionPerformed
227

228         if (jListStyles.getSelectedIndex() >= 0)
229         {
230             Style irc = null;
231             try {
232                 irc = (Style)jListStyles.getSelectedValue();
233             } catch (Exception JavaDoc ex) { return; }
234                        
235             
236             if (javax.swing.JOptionPane.showConfirmDialog(this,I18n.getString("messages.stylesDialog.removingStyle","Do you want really remove this style?"),"",
237                     javax.swing.JOptionPane.YES_NO_CANCEL_OPTION,
238                     javax.swing.JOptionPane.QUESTION_MESSAGE) == javax.swing.JOptionPane.YES_OPTION)
239             {
240                 if (!isListingReportStyles())
241                 {
242                     MainFrame.getMainInstance().getStyleLibrarySet().remove(irc);
243                     MainFrame.getMainInstance().saveStyleLibrary();
244                     getListModel().removeElement(irc);
245                 }
246                 else
247                 {
248                     Report report = MainFrame.getMainInstance().getActiveReportFrame().getReport();
249                     report.removeStyle( irc );
250                 }
251             }
252         }
253     }//GEN-LAST:event_jButtonDeleteStyleActionPerformed
254

255     private void jButtonEditStyleActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonEditStyleActionPerformed
256

257         StyleDialog cd = new StyleDialog(MainFrame.getMainInstance(),true);
258         
259         // Take the selected style from the table...
260
if (jListStyles.getSelectedValue() == null) return;
261         int index = jListStyles.getSelectedIndex();
262         Style irc = (Style)jListStyles.getSelectedValue();
263         
264         if (irc == null) return;
265         
266         cd.setStyle( irc );
267         if (!isListingReportStyles())
268         {
269             cd.setLibraryStyle(true);
270         }
271             
272         cd.setVisible(true);
273         
274         if (cd.getDialogResult() == JOptionPane.OK_OPTION)
275         {
276             Style sub = cd.getStyle();
277             // Now we have an old and a new object.
278
// 1. Adjust table...
279
try {
280                 getListModel().setElementAt(sub, index);
281             } catch (Exception JavaDoc ex) { return; }
282             
283             if (!isListingReportStyles())
284             {
285                 MainFrame.getMainInstance().saveStyleLibrary();
286             }
287         }
288         
289     }//GEN-LAST:event_jButtonEditStyleActionPerformed
290

291     private void jButtonAddStyleActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonAddStyleActionPerformed
292

293         StyleDialog cd = new StyleDialog(MainFrame.getMainInstance(),true);
294         
295         if (!isListingReportStyles())
296         {
297             cd.setLibraryStyle(true);
298         }
299             
300         cd.setVisible(true);
301         
302         if (cd.getDialogResult() == JOptionPane.OK_OPTION && cd.isLibraryStyle())
303         {
304             // If we are editing/adding a library style, no events are fire, so we have
305
// to handle the new style by our self...
306
// The new style should be already present in library.
307
getListModel().addElement( cd.getStyle() );
308             MainFrame.getMainInstance().saveStyleLibrary();
309         }
310         
311     }//GEN-LAST:event_jButtonAddStyleActionPerformed
312

313     public void languageChanged(LanguageChangedEvent evt) {
314     
315           jButtonAddStyle.setToolTipText( I18n.getString("gui.style.newStyle","New style") );
316           jButtonEditStyle.setToolTipText( I18n.getString("gui.style.editStyle","Edit style") );
317           jButtonDeleteStyle.setToolTipText( I18n.getString("gui.style.deleteStyle","Delete style") );
318           
319           for (int i=0; i<jComboBoxStyleType.getItemCount(); ++i)
320           {
321               Tag t = (Tag)jComboBoxStyleType.getItemAt(i);
322               if ( t.getName().equals("reportStyles"))
323                   t.setName( I18n.getString("gui.style.types.reportStyles","Report styles") );
324               else if ( t.getName().equals("libraryStyles"))
325                   t.setName( I18n.getString("gui.style.types.libraryStyles","Styles Library") );
326               
327           }
328     
329     }
330
331     public void reportFrameActivated(ReportFrameActivatedEvent evt) {
332         setActiveReportFrame( evt.getReportFrame() );
333     }
334
335     public JReportFrame getActiveReportFrame() {
336         return activeReportFrame;
337     }
338
339     public void setActiveReportFrame(JReportFrame newActiveReportFrame) {
340         
341         if (newActiveReportFrame != this.activeReportFrame)
342         {
343             if (this.activeReportFrame != null)
344             {
345                 this.activeReportFrame.getReport().removeStyleChangedListener( this );
346             }
347         }
348         else
349         {
350             return;
351         }
352         
353         this.activeReportFrame = newActiveReportFrame;
354         
355         if (this.activeReportFrame != null)
356         {
357             this.activeReportFrame.getReport().addStyleChangedListener( this );
358         }
359                 
360         updateStyles();
361     }
362     
363     
364     public void updateStyles()
365     {
366         
367         getListModel().removeAllElements();
368         jListStyles.setShowLibrary( !isListingReportStyles() );
369         if ( ((Tag)jComboBoxStyleType.getSelectedItem()).getValue().equals("libraryStyles"))
370         {
371             // load library styles....
372
Vector JavaDoc v = MainFrame.getMainInstance().getStyleLibrarySet();
373             for (int i=0; i<v.size(); ++i)
374             {
375                 getListModel().addElement( v.elementAt(i));
376             }
377         }
378         else if ( ((Tag)jComboBoxStyleType.getSelectedItem()).getValue().equals("reportStyles"))
379         {
380             if (getActiveReportFrame() != null)
381             {
382                 Vector JavaDoc v = getActiveReportFrame().getReport().getStyles();
383                 for (int i=0; i<v.size(); ++i)
384                 {
385                     getListModel().addElement( v.elementAt(i));
386                 }
387             }
388         }
389     }
390
391     protected DefaultListModel JavaDoc getListModel() {
392         return listModel;
393     }
394
395     protected void setListModel(DefaultListModel JavaDoc listModel) {
396         this.listModel = listModel;
397     }
398
399     public void styleChanged(StyleChangedEvent evt) {
400         
401         if (evt.getSource() == this) return;
402         updateStyles();
403     }
404     
405     
406     
407     /**
408      * Return true if the currently displayed list refers to the report styles
409      * False = displaying library styles (currently not modifiables...)
410      */

411     public boolean isListingReportStyles()
412     {
413         Tag t = (Tag)jComboBoxStyleType.getSelectedItem();
414         
415         if (t != null && t.getValue().equals("reportStyles"))
416         {
417             return true;
418         }
419         
420         return false;
421     }
422
423     
424     // Variables declaration - do not modify//GEN-BEGIN:variables
425
private javax.swing.JButton JavaDoc jButtonAddStyle;
426     private javax.swing.JButton JavaDoc jButtonDeleteStyle;
427     private javax.swing.JButton JavaDoc jButtonEditStyle;
428     private javax.swing.JComboBox JavaDoc jComboBoxStyleType;
429     private it.businesslogic.ireport.gui.style.JBGList jListStyles;
430     private javax.swing.JPanel JavaDoc jPanel1;
431     private javax.swing.JScrollPane JavaDoc jScrollPane1;
432     private javax.swing.JToolBar JavaDoc jToolBar1;
433     // End of variables declaration//GEN-END:variables
434

435     public void applyI18n(){
436                 // Start autogenerated code ----------------------
437
// End autogenerated code ----------------------
438
}
439 }
440
Popular Tags