Java tutorial
package com.softeffect.primemovers.ui; import java.util.ArrayList; import java.util.Hashtable; import java.util.Map; import java.util.Properties; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import org.openswing.swing.domains.java.Domain; import org.openswing.swing.internationalization.java.EnglishOnlyResourceFactory; import org.openswing.swing.internationalization.java.Language; import org.openswing.swing.lookup.client.LookupController; import org.openswing.swing.mdi.client.ClientFacade; import org.openswing.swing.mdi.client.Clock; import org.openswing.swing.mdi.client.DefaultToolBar; import org.openswing.swing.mdi.client.GenericStatusPanel; import org.openswing.swing.mdi.client.MDIController; import org.openswing.swing.mdi.client.MDIFrame; import org.openswing.swing.mdi.java.ApplicationFunction; import org.openswing.swing.permissions.client.LoginController; import org.openswing.swing.permissions.client.LoginDialog; import org.openswing.swing.table.profiles.client.FileGridProfileManager; import org.openswing.swing.tree.java.OpenSwingTreeNode; import org.openswing.swing.util.client.ClientSettings; import org.springframework.context.ApplicationContext; import com.softeffect.primemovers.application.ApplicationInfo; import com.softeffect.primemovers.domain.OrgProfile; import com.softeffect.primemovers.service.OrgProfileService; /** * * @author Otieno Lawrence * */ public class ClientApplication implements MDIController, LoginController { private AppClientFacade clientFacade = null; private Hashtable domains = new Hashtable(); private static ApplicationInfo applicationInfo; private static OrgProfileService orgProfileService; private static final String CONFIG_PACKAGE = "com.softeffect.primemovers.spring"; private static ApplicationContext mainContext; private static final JFrame frame = new JFrame(); public ClientApplication() { clientFacade = new AppClientFacade(); Hashtable domains = new Hashtable(); Domain statusDomain = new Domain("STATUS"); statusDomain.addDomainPair("ACTIVE", "Active"); statusDomain.addDomainPair("EXPIRED", "Expired"); domains.put(statusDomain.getDomainId(), statusDomain); Properties props = new Properties(); ClientSettings clientSettings = new ClientSettings(new EnglishOnlyResourceFactory("TZS", props, true), domains); ClientSettings.BACKGROUND = "background4.jpg"; ClientSettings.TREE_BACK = "treeback2.jpg"; ClientSettings.VIEW_BACKGROUND_SEL_COLOR = true; ClientSettings.VIEW_MANDATORY_SYMBOL = false; ClientSettings.FILTER_PANEL_ON_GRID = true; ClientSettings.ASK_BEFORE_CLOSE = true; ClientSettings.GRID_PROFILE_MANAGER = new FileGridProfileManager(); ClientSettings.LOOKUP_FRAME_CONTENT = LookupController.GRID_AND_FILTER_FRAME; ClientSettings.MDI_TOOLBAR = new DefaultToolBar(); LoginDialog d = new LoginDialog(null, false, this, "Authentication", "Login", 'L', "Exit", 'E', "Store Account", applicationInfo.getName()); } /** * Method called after MDI creation. */ public void afterMDIcreation(MDIFrame frame) { GenericStatusPanel dairyNamePanel = new GenericStatusPanel(); dairyNamePanel.setColumns(40); MDIFrame.addStatusComponent(dairyNamePanel); String dairyName = ""; if (orgProfileService.findOrgProfile() != null) { dairyName = orgProfileService.findOrgProfile().getOrgFullName(); } dairyNamePanel.setText(dairyName); GenericStatusPanel userPanel = new GenericStatusPanel(); userPanel.setColumns(20); MDIFrame.addStatusComponent(userPanel); String currentUserName = orgProfileService.findOrgProfile().getAdminUserName(); userPanel.setText("WELCOME ".concat(currentUserName.toUpperCase()).concat("!")); MDIFrame.addStatusComponent(new Clock()); } /** * @see JFrame getExtendedState method */ public int getExtendedState() { return JFrame.MAXIMIZED_BOTH; } /** * @return client facade, invoked by the MDI Frame tree/menu */ public ClientFacade getClientFacade() { return clientFacade; } /** * Method used to destroy application. */ public void stopApplication() { System.exit(0); } /** * Defines if application functions must be viewed inside a tree panel of * MDI Frame. * * @return <code>true</code> if application functions must be viewed inside * a tree panel of MDI Frame, <code>false</code> no tree is viewed */ public boolean viewFunctionsInTreePanel() { return true; } /** * Defines if application functions must be viewed in the menubar of MDI * Frame. * * @return <code>true</code> if application functions must be viewed in the * menubar of MDI Frame, <code>false</code> otherwise */ public boolean viewFunctionsInMenuBar() { return true; } /** * @return <code>true</code> if the MDI frame must show a login menu in the * menubar, <code>false</code> no login menu item will be added */ public boolean viewLoginInMenuBar() { return true; } /** * @return application title */ public String getMDIFrameTitle() { if (applicationInfo != null) { return applicationInfo.getName().concat("-").concat(applicationInfo.getVersion()); } return ""; } /** * @return text to view in the about dialog window */ public String getAboutText() { return applicationInfo.getName() + ", Version " + applicationInfo.getVersion() + "\n" + "Licensed To: " + orgProfileService.findOrgProfile().getOrgFullName() + "\n" + "Copyright: Copyright (C) " + applicationInfo.getReleaseYear() + " " + applicationInfo.getVendorName() + "\n" + applicationInfo.getVendorUrl() + "\n" + "Build Time: " + applicationInfo.getBuildTime(); } /** * @return image name to view in the about dialog window */ public String getAboutImage() { return null; } /** * @param parentFrame * parent frame * @return a dialog window to logon the application; the method can return * null if viewLoginInMenuBar returns false */ public JDialog viewLoginDialog(JFrame parentFrame) { JDialog d = new LoginDialog(frame, true, this, "Authentication", "Login", 'L', "Exit", 'E', "Store Account", applicationInfo.getName()); d.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); return d; } /** * @return maximum number of failed login */ public int getMaxAttempts() { return 3; } /** * Method called by MDI Frame to authenticate the user. * * @param loginInfo * login information, like username, password, ... * @return <code>true</code> if user is correcly authenticated, * <code>false</code> otherwise */ public boolean authenticateUser(Map loginInfo) throws Exception { OrgProfile orgProfile = orgProfileService.findOrgProfile(); String userName = loginInfo.get("username").toString(); String password = loginInfo.get("password").toString(); if (userName.equals(orgProfile.getAdminUserName()) && password.equals(orgProfile.getAdminPassword())) { return true; } else return false; } /** * * @param argv */ public static void main(String[] argv) { new MyApplicationLauncher("/spring/startup_context.xml", new String[] { CONFIG_PACKAGE }); mainContext = MyApplicationLauncher.getMainAppContext(); applicationInfo = mainContext.getBean("applicationInfo", ApplicationInfo.class); orgProfileService = mainContext.getBean("orgProfileService", OrgProfileService.class); initApp(); new ClientApplication(); } /** * Method called by LoginDialog to notify the sucessful login. * * @param loginInfo * login information, like username, password, ... */ public void loginSuccessful(Map loginInfo) { MDIFrame mdi = new MDIFrame(this); try { UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); /* Turn off metal's use of bold fonts */ UIManager.put("swing.boldMetal", Boolean.FALSE); SwingUtilities.updateComponentTreeUI(mdi); } catch (Throwable ex) { ex.printStackTrace(); } } /** * @return <code>true</code> if the MDI frame must show a change language * menu in the menubar, <code>false</code> no change language menu * item will be added */ public boolean viewChangeLanguageInMenuBar() { return true; } /** * @return list of languages supported by the application */ public ArrayList getLanguages() { ArrayList list = new ArrayList(); list.add(new Language("EN", "English")); list.add(new Language("SWA", "Kiswahili")); return list; } /** * @return application functions (ApplicationFunction objects), organized as * a tree */ public DefaultTreeModel getApplicationFunctions() { DefaultMutableTreeNode root = new OpenSwingTreeNode("Insurance Certicate Database"); DefaultTreeModel model = new DefaultTreeModel(root); ApplicationFunction n1 = new ApplicationFunction("Administration", null); ApplicationFunction n10 = new ApplicationFunction("Configuration", "", null, "getConfigurationFunction"); n1.add(n10); ApplicationFunction n2 = new ApplicationFunction("Tasks", null); ApplicationFunction n20 = new ApplicationFunction("Add Records", "", null, "getAddRecordsFunction"); ApplicationFunction n21 = new ApplicationFunction("List Records", "", null, "getListRecordsFunction"); ApplicationFunction n22 = new ApplicationFunction("Reports", "", null, "getReportsFunction"); n2.add(n20); n2.add(n21); n2.add(n22); root.add(n1); root.add(n2); return model; } /** * @return <code>true</code> if the MDI frame must show a panel in the * bottom, containing last opened window icons, <code>false</code> * no panel is showed */ public boolean viewOpenedWindowIcons() { return true; } /** * @return <code>true</code> if the MDI frame must show the "File" menu in * the menubar of the frame, <code>false</code> to hide it */ public boolean viewFileMenu() { return true; } /** * Initializes some data for first use */ private static void initApp() { if (orgProfileService.findOrgProfile() == null) { OrgProfile orgProfile = new OrgProfile(); orgProfile.setOrgFullName("Strategic System Partners"); orgProfile.setOrgShortName("SSP"); orgProfile.setAdminUserName("administrator"); orgProfile.setAdminPassword("admin"); orgProfileService.save(orgProfile); } } }