KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > swingclient > SharkClient


1 package org.enhydra.shark.swingclient;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.net.*;
6
7 import javax.swing.*;
8
9 import org.enhydra.shark.Shark;
10
11 import org.enhydra.shark.api.client.wfservice.*;
12 import org.enhydra.shark.swingclient.actions.*;
13 import org.enhydra.shark.swingclient.CommonExpressionBuilder;
14
15 /**
16  * The base abstract class for creating workflow clients (administration,
17  * monitoring, worklist handling). Implements four esential actions
18  * used by every client: Connect, Disconnect, SetRefreshingRate and
19  * Exit. It also defines the visual framework for clients.
20  * <p> It creates and starts the Thread that is used for refreshing
21  * the engine data (it calls the refresh method of this class).
22  * <p> The implementation classes has to implement some abstract
23  * methods, such as refresh, createCenterComponent, etc.
24  *
25  * @author Sasa Bojanic
26  * @version 1.0
27  */

28 public abstract class SharkClient extends ActionPanel {
29
30    // Application Icon. From resource file.
31
protected static ImageIcon appIcon;
32    // Application Icon. From resource file.
33
protected static ImageIcon logoIcon;
34    // Application Title and other stuff. From resource file.
35
protected static String JavaDoc appTitle;
36
37    // The frame of application
38
protected JFrame myFrame;
39
40    protected JMenuBar menubar;
41
42    protected SplashScreen splashScreen;
43    // periodical refreshment
44
protected RefreshingRate refreshingRateInSeconds;
45
46    protected boolean isRefresherActive=false;
47    protected boolean isRefresherEnabled=true;
48
49    protected Thread JavaDoc refresher=new Thread JavaDoc () {
50       public void run () {
51          while (true) {
52             try {
53                if (isRefresherEnabled && username!=null) {
54                   isRefresherActive=true;
55                   refresh(true);
56                }
57             } catch (Exception JavaDoc ex) {}
58             isRefresherActive=false;
59             try {
60                Thread.sleep(refreshingRateInSeconds.getRefreshingRate()*1000);
61             } catch (Exception JavaDoc ex){
62                isRefresherActive=false;
63             }
64          }
65       }
66    };
67
68    protected static SharkInterface workflowServer;
69    protected static SharkConnection workflowService;
70    protected static AdminMisc adminMiscUtilities;
71    protected static CacheAdministration cacheAdmin;
72    protected static DeadlineAdministration deadlineAdmin;
73    protected static ExecutionAdministration execAdmin;
74    protected static LimitAdministration limitAdmin;
75    protected static ApplicationMappingAdministration applicationMappingAdmin;
76    protected static ParticipantMappingAdministration participantMappingAdmin;
77    protected static ScriptMappingAdministration scriptMappingAdmin;
78    protected static PackageAdministration pkgAdmin;
79    protected static RepositoryMgr repositoryMgr;
80    protected static UserGroupAdministration userGroupAdmin;
81    protected static CommonExpressionBuilder commonExpressionBuilder;
82
83    protected static String JavaDoc username=null;
84    protected static String JavaDoc pwd=null;
85
86    protected static String JavaDoc configFileName="conf/Shark.conf";
87
88    public static SharkInterface findWorkflowServer () {
89       if (workflowServer==null) {
90          try{
91             java.io.File JavaDoc f=new java.io.File JavaDoc(SharkClient.configFileName);
92             if (f.exists()) {
93                Shark.configure(f);
94             } else {
95                Shark.configure();
96             }
97             workflowServer=Shark.getInstance();
98             return workflowServer;
99          } catch(Exception JavaDoc e) {
100             //e.printStackTrace();
101
return null;
102          }
103       } else {
104          return workflowServer;
105       }
106    }
107
108    public static AdminMisc getAdminMiscUtilities () {
109       if (adminMiscUtilities==null) {
110          adminMiscUtilities=findWorkflowServer().getAdminInterface().getAdminMisc();
111          adminMiscUtilities.connect(username);
112       }
113       return adminMiscUtilities;
114    }
115
116    public static CacheAdministration getCacheAdmin () {
117       if (cacheAdmin==null) {
118          cacheAdmin=findWorkflowServer().getAdminInterface().getCacheAdministration();
119          try {
120             cacheAdmin.connect(username);
121          } catch (Exception JavaDoc ex) {}
122       }
123       return cacheAdmin;
124    }
125
126    public static DeadlineAdministration getDeadlineAdmin () {
127       if (deadlineAdmin==null) {
128          deadlineAdmin=findWorkflowServer().getAdminInterface().getDeadlineAdministration();
129          try {
130             deadlineAdmin.connect(username);
131          } catch (Exception JavaDoc ex) {}
132       }
133       return deadlineAdmin;
134    }
135
136    public static ExecutionAdministration getExecAmin () {
137       if (execAdmin==null) {
138          execAdmin=findWorkflowServer().
139             getAdminInterface().getExecutionAdministration();
140          try {
141             execAdmin.connect(username,pwd,"","");
142          } catch (Exception JavaDoc ex) {}
143       }
144       return execAdmin;
145    }
146
147    public static LimitAdministration getLimitAdmin () {
148       if (limitAdmin==null) {
149          limitAdmin=findWorkflowServer().getAdminInterface().getLimitAdministration();
150          try {
151             limitAdmin.connect(username);
152          } catch (Exception JavaDoc ex) {}
153       }
154       return limitAdmin;
155    }
156
157    public static ParticipantMappingAdministration getParticipantMappingsAdmin () {
158       if (participantMappingAdmin==null) {
159          participantMappingAdmin=findWorkflowServer().getAdminInterface().getParticipantMappingAdministration();
160          participantMappingAdmin.connect(username);
161       }
162       return participantMappingAdmin;
163    }
164
165    public static ApplicationMappingAdministration getApplicationMappingsAdmin () {
166       if (applicationMappingAdmin==null) {
167          applicationMappingAdmin=findWorkflowServer().getAdminInterface().getApplicationMappingAdministration();
168          applicationMappingAdmin.connect(username);
169       }
170       return applicationMappingAdmin;
171    }
172
173    public static ScriptMappingAdministration getScriptMappingsAdmin () {
174       if (scriptMappingAdmin==null) {
175          scriptMappingAdmin=findWorkflowServer().getAdminInterface().getScriptMappingAdministration();
176          scriptMappingAdmin.connect(username);
177       }
178       return scriptMappingAdmin;
179    }
180
181    public static PackageAdministration getPackageAmin () {
182       if (pkgAdmin==null) {
183          pkgAdmin=findWorkflowServer().getAdminInterface().getPackageAdministration();
184          pkgAdmin.connect(username);
185       }
186       return pkgAdmin;
187    }
188
189    public static RepositoryMgr getRepositoryManager () {
190       if (repositoryMgr==null) {
191          repositoryMgr=findWorkflowServer().getRepositoryManager();
192          repositoryMgr.connect(username);
193       }
194       return repositoryMgr;
195    }
196
197    public static UserGroupAdministration getUserGroupAmin () {
198       if (userGroupAdmin==null) {
199          userGroupAdmin=findWorkflowServer().getAdminInterface().getUserGroupAdministration();
200          userGroupAdmin.connect(username);
201       }
202       return userGroupAdmin;
203    }
204
205    public static CommonExpressionBuilder getCommonExpressionBuilder () {
206       if (commonExpressionBuilder==null) {
207          ExecutionAdministration ea=getExecAmin();
208          commonExpressionBuilder=new CommonExpressionBuilder(ea,findWorkflowServer().getExpressionBuilderManager());
209       }
210       return commonExpressionBuilder;
211    }
212
213    public boolean isRefresherActive () {
214       return isRefresherActive;
215    }
216
217    public void startRefresher () {
218       isRefresherEnabled=true;
219    }
220
221    public void stopRefresher () {
222       isRefresherEnabled=false;
223    }
224
225    //****************************** DEFAULT INITIALIZATION ***********************
226
private void initResources() {
227       try {
228          String JavaDoc vers = System.getProperty("java.version");
229          if (vers.compareTo("1.4") < 0) {
230             System.out.println("!!!WARNING: application must be run with a " +
231                                   "1.4 or higher version VM!!!");
232          }
233
234          // application title
235
appTitle = ResourceManager.getLanguageDependentString(getAppTitleResourceString());
236
237          // Logo
238
URL url = ResourceManager.getResource(getLogoResourceString());
239          if (url != null) logoIcon = new ImageIcon(url);
240
241          // Icon
242
url = ResourceManager.getResource(getIconResourceString());
243          if (url != null) appIcon = new ImageIcon(url);
244
245          try {
246             //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
247
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
248          } catch (Exception JavaDoc exc) {
249             System.err.println("Error loading L&F: " + exc);
250          }
251       } catch (Throwable JavaDoc t) {
252          System.err.println("uncaught exception: " + t);
253          t.printStackTrace();
254       }
255    }
256    //*********************** END OF DEFAULT INITIALIZATION ***********************
257

258    //************************** CONSTRUCTOR ***********************
259
/** Creates instance of main application class. */
260    public SharkClient (JFrame frame) {
261       super();
262       myFrame = frame;
263       splashScreen=new SplashScreen(frame);
264
265       super.init();
266       initResources();
267       int rris;
268       try {
269          rris=Integer.parseInt(
270             ResourceManager.getLanguageDependentString(getRefreshingRateString()));
271       } catch (Exception JavaDoc ex) {
272          rris=50;
273       }
274       refreshingRateInSeconds=new RefreshingRate(frame,rris);
275
276
277       menubar = BarFactory.createMenubar(getMenubarResourceString(),commands);
278       // adding menubar to the main panel
279
add(menubar,BorderLayout.NORTH);
280       // adding center component
281
add(createCenterComponent(),BorderLayout.CENTER);
282
283       myFrame.setBackground(Color.lightGray);
284       myFrame.getContentPane().setLayout(new BorderLayout());
285       myFrame.getContentPane().add(this,BorderLayout.CENTER);
286       myFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
287       myFrame.addWindowListener(new AppCloser());
288       myFrame.pack();
289       // set default location to be centered and size to be almost maximized
290
//Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
291
Rectangle screenSize = GraphicsEnvironment
292          .getLocalGraphicsEnvironment()
293          .getDefaultScreenDevice()
294          .getDefaultConfiguration()
295          .getBounds();
296
297       int xMinus=50, yMinus=100;
298       myFrame.setBounds(xMinus/2, yMinus/2, screenSize.width - xMinus,
299                         screenSize.height - yMinus);
300
301       if (appIcon != null) myFrame.setIconImage(appIcon.getImage());
302       myFrame.setTitle(appTitle+" - "+ResourceManager.getLanguageDependentString("NoneKey"));
303
304       myFrame.setVisible(true);
305
306       refresher.start();
307       getAction(Utils.getUnqualifiedClassName(Connect.class)).actionPerformed(null);
308       if (username==null) {
309          System.exit(0);
310       }
311       requestFocus();
312    }
313    //************************* END OF WORKLISTHANDLER CONSTRUCTOR *****************
314

315    //************** APPCLOSER CLASS FOR CLOSING APPLICATION WINDOW ***************
316
/**
317     * To shutdown when run as an application.
318     */

319    protected final class AppCloser extends WindowAdapter {
320       public void windowClosing(WindowEvent e) {
321          getAction(Utils.getUnqualifiedClassName(Exit.class)).actionPerformed(null);
322       }
323    }
324    //**** END OF CREATING APPLICATION CLOSER COMPONENT FOR APPLICATION WINDOW ****
325

326    protected void createActions () {
327       defaultActions=new Action[] {
328          new Connect(this),
329             new Disconnect(this),
330             new ShutdownEngine(this),
331             new Refresh(this),
332             new SetRefreshingRate(this),
333             new Exit(this)
334       };
335       defaultActions[2].setEnabled(false);
336    }
337
338    public void setTitle () {
339       myFrame.setTitle(appTitle+" - "+getFullUserName());
340    }
341
342    public String JavaDoc getFullUserName () {
343       String JavaDoc name=ResourceManager.getLanguageDependentString("NoneKey");
344       if (username!=null) {
345          try {
346             name=getUserGroupAmin().getUserRealName(username);
347             if (name==null || name.trim().length()==0) {
348                name=username;
349             }
350          } catch (Exception JavaDoc ex) {
351          }
352       }
353       return name;
354    }
355
356    public JFrame getFrame () {
357       return myFrame;
358    }
359
360    public SplashScreen getSplashScreen () {
361       return splashScreen;
362    }
363
364    public static String JavaDoc getAppTitle () {
365       return appTitle;
366    }
367
368    public static SharkConnection getService () {
369       return workflowService;
370    }
371
372    public static void setService (SharkConnection wcs) {
373       workflowService=wcs;
374       if (wcs==null) {
375          execAdmin=null;
376          commonExpressionBuilder=null;
377       }
378    }
379
380    public static String JavaDoc getUsername () {
381       return username;
382    }
383
384    public static void setUsername (String JavaDoc username) {
385       SharkClient.username=username;
386    }
387
388    public static void setPassword (String JavaDoc pwd) {
389       SharkClient.pwd=pwd;
390    }
391
392    public void enableControls (boolean enable) {
393       // enable/disable menubar
394
for (int i=0; i<menubar.getComponentCount(); i++) {
395          menubar.getComponent(i).setEnabled(enable);
396       }
397    }
398
399    public RefreshingRate getRefreshingRate () {
400       return refreshingRateInSeconds;
401    }
402
403    protected abstract String JavaDoc getAppTitleResourceString();
404    protected abstract String JavaDoc getLogoResourceString();
405    protected abstract String JavaDoc getIconResourceString();
406    protected abstract String JavaDoc getMenubarResourceString();
407    protected abstract String JavaDoc getRefreshingRateString();
408    public abstract void clearComponents();
409
410    public abstract void refresh (boolean mandatoryRefreshing);
411
412 }
413
414
Popular Tags