KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > irplugin > IRPlugin


1 /*
2  * IRPlugin.java
3  *
4  * Created on March 31, 2006, 5:16 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package com.jaspersoft.jasperserver.irplugin;
11
12 import com.jaspersoft.jasperserver.irplugin.gui.CheckUpdateDialog;
13 import com.jaspersoft.jasperserver.irplugin.gui.RepositoryExplorer;
14 import it.businesslogic.ireport.gui.MainFrame;
15 import java.io.File JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.util.Properties JavaDoc;
19 import javax.swing.JOptionPane JavaDoc;
20 import javax.swing.SwingUtilities JavaDoc;
21
22 /**
23  *
24  * @author Administrator
25  */

26 public class IRPlugin extends it.businesslogic.ireport.plugin.IReportPlugin {
27     
28     public static String JavaDoc configFileName = "jasperserverplugin.xml";
29     public static final String JavaDoc REQUIRED_VERSION = "1.1.0";
30     public static final String JavaDoc CURRENT_VERSION = "1.1.0";
31     public static final String JavaDoc PROPERTY_CHECK_FOR_UPDATE = "PROPERTY_CHECK_FOR_UPDATE";
32     
33     private boolean viewVisible = false;
34     
35     private boolean firstTime = true;
36     
37     private static IRPlugin mainInstance = null;
38     public static IRPlugin getMainInstance() {
39         return mainInstance;
40     }
41     
42     /** This is the complete list of servers available on the tree.
43      * After an add or a remove, please save the configuration and update
44      * the repositoryExplorer.
45      */

46     private java.util.List JavaDoc jServers = new java.util.ArrayList JavaDoc();
47     
48     /**
49      * Properties are stored in the user home directory under .ireport/jasperserverplugin.xml
50      */

51     private java.util.Properties JavaDoc properties = new java.util.Properties JavaDoc();
52     
53     
54     private RepositoryExplorer repositoryExplorer = null;
55     boolean firstCall = true;
56     
57     /** Creates a new instance of HelloWorld */
58     public IRPlugin() {
59         mainInstance = this;
60     }
61     
62     public void call() {
63         
64         // checkIReportVersion();
65
if (getRepositoryExplorer() == null)
66         {
67              loadConfiguration();
68              setRepositoryExplorer(new RepositoryExplorer(this));
69         }
70         
71         if (MainFrame.getMainInstance().constTitle.compareTo("iReport 1.2.6 ") < 0)
72          {
73          // try {
74
//
75
// getMainFrame().getDockingContainerLeft().insertPanel(0,"JasperServer",getRepositoryExplorer(), DockingContainer.INSERT_MODE_SHAREDPOSTION,true);
76
//
77
// } catch (Exception ex) { };
78

79              SwingUtilities.invokeLater( new Runnable JavaDoc()
80              {
81                  public void run()
82                  {
83                     JOptionPane.showMessageDialog(null,"JasperReports plugin requires iReport 1.2.6 or above!");
84                  }
85              }
86              );
87                     return;
88         }
89         else
90         {
91             if (!isViewVisible())
92             {
93                 DockingUtil.installUI(getRepositoryExplorer());
94             }
95         }
96         
97         getMainFrame().logOnConsole("Checking? " + (!firstTime || getProperties().getProperty(PROPERTY_CHECK_FOR_UPDATE, "true").equals("true")));
98         if (!firstTime || getProperties().getProperty(PROPERTY_CHECK_FOR_UPDATE, "true").equals("true"))
99         {
100             SwingUtilities.invokeLater( new Runnable JavaDoc()
101             {
102             
103                 public void run()
104                 {
105                     try {
106                             UpgradeSearch us = new UpgradeSearch();
107                             Thread JavaDoc t = new Thread JavaDoc(us);
108                             t.run();
109                         } catch (Throwable JavaDoc ex) {
110                             ex.printStackTrace();
111                         }
112                 }
113             });
114        }
115        firstTime = false;
116         
117     }
118     
119     
120     
121     public void configure() {
122         CheckUpdateDialog cup = new CheckUpdateDialog(getMainFrame(), true);
123         cup.setVisible(true);
124     }
125     
126     
127     /**
128      * Load the plugin configuration from IREPORT_USER_HOME_DIR/jasperserverplugin.xml
129      */

130     public void loadConfiguration()
131     {
132         setProperties(new java.util.Properties JavaDoc());
133         File JavaDoc configFile = new File JavaDoc( MainFrame.IREPORT_USER_HOME_DIR, configFileName);
134         try {
135             
136             if (configFile.exists())
137             {
138                 getProperties().loadFromXML( new FileInputStream JavaDoc( configFile ) );
139             }
140         } catch (Exception JavaDoc ex)
141         {
142             ex.printStackTrace();
143         }
144         
145         Properties JavaDoc props = getProperties();
146         for (int i=0;props.getProperty("server."+i+".name") != null; ++i)
147         {
148             JServer server = new JServer();
149             server.setName( props.getProperty("server."+i+".name") );
150             server.setUrl( props.getProperty("server."+i+".url") );
151             server.setUsername( props.getProperty("server."+i+".username") );
152             server.setPassword( props.getProperty("server."+i+".password") );
153             
154             getJServers().add(server);
155         }
156     }
157     
158     /**
159      * Save the plugin configuration in IREPORT_USER_HOME_DIR/jasperserverplugin.xml
160      */

161     public boolean saveConfiguration()
162     {
163         File JavaDoc dir = new File JavaDoc( MainFrame.IREPORT_USER_HOME_DIR );
164         
165         // Update all informations about servers...
166
// 1. remove all server.X tags...
167
// 2. add all server configurations...
168
for (int i=0; getProperties().getProperty("server."+i+".name") != null; ++i)
169         {
170             getProperties().remove("server."+i+".name");
171             getProperties().remove("server."+i+".url");
172             getProperties().remove("server."+i+".username");
173             getProperties().remove("server."+i+".password");
174         }
175         
176         for (int i=0; i<getJServers().size(); ++i)
177         {
178             JServer server = (JServer)getJServers().get(i);
179             getProperties().setProperty("server."+i+".name", server.getName());
180             getProperties().setProperty("server."+i+".url", server.getUrl());
181             getProperties().setProperty("server."+i+".username", server.getUsername());
182             getProperties().setProperty("server."+i+".password", server.getPassword());
183         }
184         
185         try {
186             if (dir.exists()) {
187                 if (!dir.isDirectory() ) {
188                     javax.swing.JOptionPane.showMessageDialog( getMainFrame(), dir.getPath() +" is not a directory!\nPlease rename this file and retry to save config!","",JOptionPane.ERROR_MESSAGE);
189                     return false;
190                 }
191             }
192             else {
193                 dir.mkdirs();
194             }
195             
196             File JavaDoc configFile = new File JavaDoc(dir,configFileName);
197             getProperties().storeToXML(new FileOutputStream JavaDoc(configFile),"JasperServer Plugin configuration file\n (c) 2006 by JasperSoft");
198             
199             return true;
200         } catch (Exception JavaDoc ex) {
201             javax.swing.JOptionPane.showMessageDialog( getMainFrame(), "An error is occurred saving Plugin config:\n"+ex.getMessage()+"\nPlease try to save config again!","",JOptionPane.ERROR_MESSAGE);
202             return false;
203         }
204     }
205
206     /**
207      * Get Plugin properties
208      */

209     public java.util.Properties JavaDoc getProperties() {
210         return properties;
211     }
212
213     /**
214      * Set Plugin properties (this nethod should be never used....
215      */

216     public void setProperties(java.util.Properties JavaDoc properties) {
217         this.properties = properties;
218     }
219
220     public java.util.List JavaDoc getJServers() {
221         return jServers;
222     }
223
224     public void setJServers(java.util.List JavaDoc jServers) {
225         this.jServers = jServers;
226     }
227     
228     /**
229      * Create a tmp file name. A complete path name is returned.
230      * The location of the file is the "jstmp" directory inside
231      * the IREPORT_USER_HOME_DIR
232      * (i.e. C:\Documents and Settings\gtoffoli\.ireport\jstmp\...)
233      * If this directory does not exist, it is created.
234      *
235      * filePrefix (can be null)
236      * fileExtension (can be null, default ".tmp")
237      *
238      * The caller is responsable for delation of this files.
239      * No check if the file already exists is performed.
240      */

241     public static String JavaDoc createTmpFileName(String JavaDoc filePrefix, String JavaDoc fileExtension)
242     {
243             if (filePrefix == null) filePrefix = "";
244             else filePrefix += "_";
245             if (fileExtension != null && !fileExtension.startsWith("."))
246             {
247                 fileExtension = "." + fileExtension;
248             }
249             
250             if (fileExtension == null) fileExtension = ".tmp";
251             
252             String JavaDoc tmpDirectory = MainFrame.getMainInstance().IREPORT_USER_HOME_DIR;
253             tmpDirectory += File.separator + "jstmp";
254             
255             File JavaDoc tmpDirectoryFile = new File JavaDoc(tmpDirectory);
256             if (!tmpDirectoryFile.exists())
257             {
258                 tmpDirectoryFile.mkdirs();
259             }
260             
261             return tmpDirectoryFile + File.separator + filePrefix + (new java.util.Date JavaDoc()).getTime() + fileExtension;
262             
263     }
264
265     public RepositoryExplorer getRepositoryExplorer() {
266         return repositoryExplorer;
267     }
268
269     public void setRepositoryExplorer(RepositoryExplorer repositoryExplorer) {
270         this.repositoryExplorer = repositoryExplorer;
271     }
272
273     public boolean isViewVisible() {
274         return viewVisible;
275     }
276
277     public void setViewVisible(boolean viewVisible) {
278         this.viewVisible = viewVisible;
279     }
280 }
Popular Tags