//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.tools.properties;
import com.nexirius.framework.application.Application;
import com.nexirius.framework.application.DialogManager;
import com.nexirius.framework.datamodel.*;
import com.nexirius.framework.dataviewer.DataViewer;
import com.nexirius.framework.dataviewer.HTMLViewerCreator;
import com.nexirius.framework.dataviewer.ViewerCreatorMap;
import com.nexirius.framework.swing.SwingViewerCreator;
import com.nexirius.tools.properties.dataviewer.MainViewer;
import com.nexirius.tools.properties.layout.EntryArrayLayout;
import com.nexirius.tools.properties.layout.EntryLayout;
import com.nexirius.tools.properties.model.EntryArrayModel;
import com.nexirius.tools.properties.model.EntryModel;
import com.nexirius.tools.properties.model.MainModel;
import com.nexirius.tools.properties.model.SettingsModel;
import com.nexirius.util.XFile;
import javax.swing.*;
import java.awt.*;
public class EditProperties extends Application {
public static XFile SETTINGS_FILE = new XFile("PEditSettings.txt");
private static EditProperties instance;
MainModel mainModel;
public EditProperties(String argv[]) {
super(argv);
instance = this;
}
public static EditProperties getInstance() {
return instance;
}
public DataModel getApplicationModel() {
return mainModel;
}
public MainModel getMainModel() {
return mainModel;
}
public SettingsModel getSettingsModel() {
return mainModel.getSettings();
}
public void exit() {
saveSettings(mainModel.getSettings());
if (mainModel.exit()) {
super.exit();
}
}
public String getApplicationName() {
return "EditProperties";
}
public void preInit() {
mainModel = new MainModel();
mainModel.addTitleListener(new TitleListener());
// append the exit command to the main model
mainModel.appendMethod(new ExitCommand());
// attach the about command to the main model
mainModel.appendMethod(new AboutCommand());
mainModel.addDataModelListener(new RelayoutListener());
}
public void init() {
SettingsModel settings = new SettingsModel();
readSettings(settings);
mainModel.initSettings(settings);
String[] locales = getMainModel().getSettings().getLocales().getArray();
ViewerCreatorMap map = getFactory().getViewerCreatorMap();
map.register(EntryModel.class, new EntryLayout(locales), true);
map.register(EntryArrayModel.class, new EntryArrayLayout(locales), false);
map.register(MainModel.class, new SwingViewerCreator(MainViewer.class));
try {
initMainPanel();
getMainFrame().pack();
DialogManager.center(getMainFrame(), true);
} catch (Exception ex) {
ex.printStackTrace();
}
getMainFrame().setIconImage(((ImageIcon)getClientResource().getIcon("application")).getImage());
}
private void readSettings(SettingsModel settings) {
if (SETTINGS_FILE.canRead()) {
try {
settings.dropData(new String(SETTINGS_FILE.getBytes()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void saveSettings(SettingsModel settings) {
try {
settings.setLastFile(mainModel.getOpenFileName());
SETTINGS_FILE.writeText(settings.dragData()) ;
} catch (Exception e) {
e.printStackTrace();
}
}
private void initMainPanel() throws Exception {
// create an editor for the main model
DataViewer editor = getFactory().createViewer(mainModel, true);
// access the Swing JComponents
JComponent editorComponent = editor.getJComponent();
// insert generated JComponents into the application window
getMainPanel().setLayout(new BorderLayout());
getMainPanel().add(editorComponent, BorderLayout.CENTER);
SwingUtilities.invokeLater(new Repainter());
}
public class Repainter implements Runnable {
public void run() {
getMainPanel().revalidate();
}
}
public String getApplicationTitle() {
return getClientResource().getLabel("applicationName") + " " + getClientResource().getLabel("applicationVersion") + " [" + mainModel.getTitle() + (mainModel.needSaving() ? "*" : "") + "]";
}
public void postInit() {
getMainModel().openFile(getMainModel().getSettings().getLastFile());
}
class ExitCommand extends DefaultDataModelCommand {
public ExitCommand() {
super("Exit");
}
public void doAction() {
exit();
}
}
class AboutCommand extends DefaultDataModelCommand {
public AboutCommand() {
super("About");
}
public void doAction() {
System.out.println("About");
StringModel about = new StringModel("");
try {
XFile aboutFile = new XFile("EditProperties.htm");
about.setText(new String(aboutFile.getBytes()));
} catch (Exception ex) {
about.setText(ex.getClass().getName());
ex.printStackTrace();
}
DialogManager.getPopupEditorAdaptor().noDuplicatePopupEdit(about, new HTMLViewerCreator(false), null);
}
}
public static void main(String argv[]) {
String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try {
UIManager.setLookAndFeel(plaf);
} catch (Exception e) {
System.out.println("Can't set '" + plaf + "' look and feel");
System.exit(0);
}
new EditProperties(argv).run();
}
class TitleListener extends DataModelAdaptor {
public void dataModelChangeValue(DataModelEvent event) {
updateApplicationTitle();
}
}
class RelayoutListener extends DataModelAdaptor {
public void dataModelEdit(DataModelEvent event) {
if (event.getId() == DataModelEvent.CALL_METHOD) {
if (event.getData().equals("relayout")) {
getMainPanel().remove(0);
try {
initMainPanel();
} catch (Exception e) {
e.printStackTrace();
}
getMainPanel().repaint();
}
}
}
}
}