Thema dieser Dokumentation / Das FirstSpirit 5 Modul- / Komponenten-Modell / Beispiel: Modul-Implementierung einer Komponente vom Typ WebApp / FirstSpirit-spezifische Klassen / WebAppConfiguration.java

WebAppConfiguration.java

In dieser Klasse wird die Konfigurationsoberfläche für das Modul definiert. Es besteht aus einem einfachen Swing-Layout und enthält zwei Textfelder, über welche die Werte der configuration.properties Datei verändert werden können. Die Implementierung einer Konfigurationsklasse ist optional, kann also entfallen, sofern eine WebApp-Komponente keine Konfigurationsmöglichkeiten zur Verfügung stellen soll.

Die main-Methode dient lediglich der einfacheren GUI-Entwicklung, muss also nicht zwingend implementiert werden.

Listing: Beispiel WebApp – Implementierung WebAppConfiguration

package de.espirit.firstspirit.opt.examples.webapp.configuration;

import de.espirit.common.Logging;
import de.espirit.firstspirit.io.FileHandle;
import de.espirit.firstspirit.module.Configuration;
import de.espirit.firstspirit.module.WebEnvironment;
import net.java.dev.designgridlayout.DesignGridLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.List;

/**
 * Implements the configuration dialog
 */
public class WebAppConfiguration implements Configuration<WebEnvironment> {

/**
* name of the configuration file
*/
public static final String CONFIGURATION_FILE = "configuration.properties";

/**
* name of the configuration property
*/
public static final String PROP_ENVIRONMENT = "environment";
public static final String PROP_COUNTER_INITAL_VALUE = "counterInitialValue";

private final Properties _config;
privateWebEnvironment _env;
private JComponent _gui;

private JTextField _environment;
private JTextField _counterInitalValue;

public WebAppConfiguration() {
_config = new Properties();
}

public boolean hasGui() {
return true;  // We want to have a GUI
}

public JComponent getGui(final Frame frame) {
if(_gui == null) {
final JPanel mainPanel = new JPanel();
final DesignGridLayout layout = new DesignGridLayout(mainPanel);
mainPanel.setOpaque(false);

final JLabel headline1 = new JLabel("WebApp Configuration");
final Font font = headline1.getFont();
headline1.setFont(font.deriveFont(font.getStyle() ^ Font.BOLD));
layout.row().left().fill().add(headline1);

final JLabel lEnvironment = new JLabel("Environment");
_environment = new JTextField(_config.getProperty(PROP_ENVIRONMENT));
layout.row().grid(lEnvironment).add(_environment, 4);

final JLabel lInitalCount = new JLabel("Counter Inital Value");
_counterInitalValue = new TextField(_config.getProperty(
PROP_COUNTER_INITAL_VALUE));
layout.row().grid(lInitalCount).add(_counterInitalValue, 4);

_gui = mainPanel;
}
return _gui;
}

/**
* load configuration from configuration file
*/
public void load() {

//set default value to PRODUCTION
_config.setProperty(PROP_ENVIRONMENT,"PRODUCTION");
_config.setProperty(PROP_COUNTER_INITAL_VALUE,"0");
try {
final FileHandle iniFile = getEnvironment().getConfDir()
.obtain(CONFIGURATION_FILE);
if(iniFile.isFile()) {
_config.load(iniFile.load());
}
} catch (IOException e) {
Logging.logWarning("Couldn't load configuration file!",
e, WebAppConfiguration.class);
}
if(_gui != null) {
_environment.setText(_config.getProperty(PROP_ENVIRONMENT));
_counterInitalValue.setText(_config.getProperty(
PROP_COUNTER_INITAL_VALUE));
}
}

/**
* read values from gui and store them into the configuration file
*/
public void store() {
if(_gui != null) {
_config.setProperty(PROP_ENVIRONMENT, _environment.getText());
_config.setProperty(PROP_COUNTER_INITAL_VALUE,
_counterInitalValue.getText());
}

try {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
_config.store(out, "FirstSpirit WebApp Example Module - Configuration");
final FileHandle iniFile =getEnvironment().getConfDir()
.obtain(CONFIGURATION_FILE);
iniFile.save(new ByteArrayInputStream(out.toByteArray()));
} catch (IOException e) {
Logging.logWarning("Couldn't store configuration file!",
e, WebAppConfiguration.class);
}
}

public Set<String> getParameterNames() {
final Set<String> result = new HashSet<String>();
for (final Enumeration<?> names = _config.propertyNames();
names.hasMoreElements();) {
result.add(names.nextElement().toString());
}
return result;
}

public String getParameter(final String name) {
return _config.getProperty(name);
}

public void init(final String moduleName, final String componentName,
final WebEnvironment env) {
_env = env;
}

public WebEnvironment getEnvironment() {
return _env;
}

/**
 * Main method for easier gui development.
 *
 * @param args   unused.
 */
public static void main(final String[] args) {
final JFrame frame = new JFrame("Configuration");
final WebAppConfiguration configurator = new WebAppConfiguration();
final JComponent gui = configurator.getGui(frame);
final JButton closeBtn = new JButton("Close");
closeBtn.addActionListener(
new ActionListener() {
public void actionPerformed(final ActionEvent e) {
frame.setVisible(false);
frame.dispose();
}
}
);

frame.getContentPane().add(gui);
frame.getRootPane().setDefaultButton(closeBtn);
frame.setSize(580, 300);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}

© 2005 - 2024 Crownpeak Technology GmbH | Alle Rechte vorbehalten. | FirstSpirit 2024.13 | Datenschutz