How to write an application parameter manager simple on the desktop?
Motivation:
The document learns to you how to create a persistent parameter manager easy on the desktop by the UJO Framework running on the Java 5.0+ SE. Parameter values will be saved in a Java Resource Bundle text format. We will create two classes only, no table model:- business class Parameters
- window component extends a JFrame class.
Click on the image to run the sample by Java Web Start
Business object
The sole business class is called Parameters and every one static property constant represents a one parameter. We want to edit each value of each parameter on a separate table row. Note that enumerator data type is supported too.package org; import java.awt.Color; import java.awt.Rectangle; import java.util.Calendar; import java.util.Locale; import org.ujoframework.implementation.map.MapProperty; import org.ujoframework.implementation.map.MapUjo; public class Parameters extends MapUjo { /** The SysTray action on a second mouse click. */ public enum Action { NONE, EVENT, HIDE; } /** Localization */ public static final MapProperty<Parameters,Locale> P_LANG = newProperty("Language", Locale.getDefault()); /** Working Hours */ public static final MapProperty<Parameters,Float> P_WORKING_HOURS = newProperty("WorkingHours", 8f); /** The First Day of the Week Day. */ public static final MapProperty<Parameters,Integer> P_FIRST_DAY_OF_WEEK = newProperty("FirstDayOfWeek", Calendar.getInstance().getFirstDayOfWeek()); /** Decimal time format. */ public static final MapProperty<Parameters,Boolean> P_DECIMAL_TIME_FORMAT = newProperty("DecimalTimeFormat", true); /** The Main selecton format. */ public static final MapProperty<Parameters,String> P_DATE_MAIN_FORMAT = newProperty("DateMainFormat", "EE, yyyy/MM/dd' Week: 'ww"); /** The Export Date Selection. */ public static final MapProperty<Parameters,String> P_DATE_REPORT_FORMAT = newProperty("DateReportFormat", P_DATE_MAIN_FORMAT.getDefault() ); /** The Export Date Selection. */ public static final MapProperty<Parameters,String> P_DATE_REPORT_FORMAT2 = newProperty("DateReportFormat2", "d'<br/><span class=\"smallMonth\">'MMMM'</span>'" ); /** The complementary report CSS style. */ public static final MapProperty<Parameters,String> P_REPORT_CSS = newProperty("ReportCSS", "styles/style.css" ); /** The Goto Date format. */ public static final MapProperty<Parameters,String> P_DATE_GOTO_FORMAT = newProperty("DateGotoFormat", "yyyy/MM/dd"); /** A Color of a private project. */ public static final MapProperty<Parameters,Color> P_COLOR_PRIVATE = newProperty("ColorOfPrivateProject", new Color(0x5DA158)); /** A Color of finished project. */ public static final MapProperty<Parameters,Color> P_COLOR_FINISHED_PROJ = newProperty("ColorOfFinishedProject", new Color(0xA9AC88)); /** A Color of an editable area. */ public static final MapProperty<Parameters,Color> P_COLOR_EDITABLE = newProperty("ColorOfEditableArea", new Color(0xFFFACD)); /** Action on a second click */ public static final MapProperty<Parameters,Action> P_SYSTRAY_SECOND_CLICK = newProperty("SystemTraySecondClick", Action.NONE); /** Modify value of finished project or task. */ public static final MapProperty<Parameters,Boolean> P_MODIFY_FINESHED_PROJ = newProperty("ModifyFinishedProject", false); /** Create a new Event on an EXIT action. */ public static final MapProperty<Parameters,Boolean> P_EXIT_EVENT_CREATE = newProperty("ExitEventCreating", Boolean.TRUE); /** Description of an EXIT action. */ public static final MapProperty<Parameters,String> P_EXIT_EVENT_DESCR = newProperty("ExitEventDescription", "EXIT"); /** Hide Buoon Icon. */ public static final MapProperty<Parameters,Boolean> P_HIDE_ICONS = newProperty("HideButtonIcons", false); /** Last window size and position. */ public static final MapProperty<Parameters,Rectangle> P_WINDOW_SIZE = newProperty("WindowSize", new Rectangle(0, 0, 622, 405)); /** Restore a last application window size and position. */ public static final MapProperty<Parameters,Boolean> P_WINDOW_SIZE_RESTORATION = newProperty("WindowSizeRestoration", true); }
Window implementation
A main window is an extension of JFrame from a Swing library. The Source code is very simple to learn, however some areas are not written by a code guideline.Take note, how an instance of table model is created by a UjoTableModel class, all visible table columss are ordered by its constructor.
Parameters object is retrieved by a UjoManagerRBundle class.
See a source code for details.
package org; import javax.swing.*; import java.awt.event.*; import java.io.File; import org.ujoframework.core.UjoManagerRBundle; import org.ujoframework.swing.SingleUjoTabModel; import org.ujoframework.swing.UjoPropertyRow; import static org.ujoframework.swing.UjoPropertyRow.*; /** Parameter manager. */ public class ParamFrame extends JFrame implements ActionListener, Runnable { private Parameters parameters; private File dataFile = new File(System.getProperty("user.home"),"ujo-param.properties"); private SingleUjoTabModel model; private JTable table; /** Creates a new instance of TableFrame */ public ParamFrame() { initComponents(); // Create a TableModel: parameters = loadParameters(); model = new SingleUjoTabModel(parameters, P_NAME, P_CLASSNAME, P_VALUE, P_DEFAULT); table.setModel(model); // Register a Close Listener: Runtime.getRuntime().addShutdownHook(new Thread(this)); } public void run() { saveParameters(); } /** Load company from file. */ private Parameters loadParameters() { if (dataFile.isFile()) try { return UjoManagerRBundle.getInstance(Parameters.class).loadResourceBundle(dataFile, false, this); } catch (Throwable e) { e.printStackTrace(); } return new Parameters(); } /** Save parameter to file. */ private void saveParameters() { try { final String msg = "Configuration file:" ; UjoManagerRBundle.getInstance(Parameters.class).saveResourceBundle(dataFile, parameters, msg, this); } catch (Throwable e) { e.printStackTrace(); } } /** Button Actions */ public void actionPerformed(ActionEvent e) { String label = ((JButton)e.getSource()).getText(); int index = table.getSelectedRow(); if ("Default".equals(label) && index>=0) { UjoPropertyRow row = model.getRow(index); model.setValueAt(row.getProperty().getDefault(), index, UjoPropertyRow.P_VALUE); } } /** Init GUI Components */ private void initComponents() { table = new JTable(); getContentPane().add(new JScrollPane(table), java.awt.BorderLayout.CENTER); JPanel panel = new JPanel(new java.awt.GridLayout(5, 1, 0, 1)); getContentPane().add(panel, java.awt.BorderLayout.EAST); addButtons(panel, "Default"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Application parameters"); pack(); } private void addButtons(JPanel panel, String... labels) { for(String label: labels) { JButton button = new JButton(label); button.addActionListener(this); panel.add(button); } } public static void main(String args[]) { new ParamFrame().setVisible(true); } }
Resource Bundle file:
The content of the Resource Bundle file is here:#Configuration file: #Tue Aug 05 21:49:55 CEST 2008 FirstDayOfWeek=2 ColorOfFinishedProject=A9AC88 HideButtonIcons=false ColorOfPrivateProject=5DA158 ColorOfEditableArea=FFFACD ModifyFinishedProject=false DateMainFormat=EE, yyyy/MM/dd' Week\: 'ww WorkingHours=8.0 ReportCSS=styles/style.css ExitEventCreating=true WindowSizeRestoration=true DateGotoFormat=yyyy/MM/dd SystemTraySecondClick=NONE DecimalTimeFormat=true Language=cs-CZ ExitEventDescription=EXIT DateReportFormat=EE, yyyy/MM/dd' Week\: 'ww WindowSize=-1,-1,622,405 DateReportFormat2=d'<br/><span class\="smallMonth">'MMMM'</span>'
Download
Cut and paste the sample code from HTML page to 3 files in a org folder via system clipboard.See a UJO Framework description for more informations.
About Author:
- You can write questions or other messages to me by a blog http://ujoframework.blogspot.com/.
- There exists some more free applications on an authors home page http://ponec.net/ .
- A contact e-mail: ujoframework@gmail.com
PPone(c) 2007-2008
Parameter manager