Java tutorial
package de.fhg.iais.asc.ui.parts; /****************************************************************************** * Copyright 2011 (c) Fraunhofer IAIS Netmedia http://www.iais.fraunhofer.de * * ************************************************************************** * * Licensed under the Apache License, Version 2.0 (the "License"); you may * * not use this file except in compliance with the License. * * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * ******************************************************************************/ import static de.fhg.iais.asc.commons.AscConfiguration.FORMAT; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Observable; import javax.swing.ButtonGroup; import javax.swing.ComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; import org.apache.commons.lang.ObjectUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import de.fhg.iais.asc.commons.AscConfiguration; import de.fhg.iais.asc.sipmaker.SipMakerFactory; import de.fhg.iais.asc.sipmaker.SipMakerKey; import de.fhg.iais.asc.ui.MyCortexModel; import de.fhg.iais.asc.ui.i18n.LocalizedUI; import de.fhg.iais.asc.ui.windows.TransformationURLWindow; import de.fhg.iais.commons.constants.Serial; public class TransformersPanel extends JPanel implements MyCortexUIComponent { private static final Logger LOG = LoggerFactory.getLogger(TransformersPanel.class); private static final String DEFAULT_TRANSFORMER_URL = "http://localhost:9997"; private static final long serialVersionUID = Serial.GLOBAL_DEFAULT_SERIAL_VERSION_UID; private JComboBox sourceMetadataComboBox; private JComboBox domainComboBox; private JComboBox providermappingComboBox; private JComboBox collectionmappingComboBox; private JComboBox versionComboBox; private JRadioButton localTransformersRadioButton; private JRadioButton remoteTransformersRadioButton; private final SipMakerFactory sipMakerFactory; private final MyCortexModel myCortexModel; public TransformersPanel(MyCortexModel myCortexModel, SipMakerFactory sipMakerFactory) { super(new GridLayout(2, 1)); this.sipMakerFactory = sipMakerFactory; init(myCortexModel); this.myCortexModel = myCortexModel; myCortexModel.addObserver(this); update(myCortexModel, null); } private void init(MyCortexModel myCortexModel) { this.add(createSelectSipMakerBoxes()); this.add(createSelectTransformersRadioGroup()); } private JPanel createSelectSipMakerBoxes() { TopLabeledElementJPanelBuilder builder = new TopLabeledElementJPanelBuilder("Transform"); this.sourceMetadataComboBox = new JComboBox(this.sipMakerFactory.getMDFormats()); builder.add("Metadata_format", this.sourceMetadataComboBox); this.domainComboBox = new JComboBox(); builder.add("Domain", this.domainComboBox); this.providermappingComboBox = new JComboBox(); builder.add("Provider_Mapping", this.providermappingComboBox); this.collectionmappingComboBox = new JComboBox(); builder.add("Collection_Mapping", this.collectionmappingComboBox); this.versionComboBox = new JComboBox(); builder.add("Version", this.versionComboBox); retrieveVersionsAndSetMaxVersion(); // The Listeners listen for changes in the selections of the comboboxes this.sourceMetadataComboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { retrieveDomains(); retrieveVersionsAndSetMaxVersion(); } }); this.domainComboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { retrieveProviders(); retrieveVersionsAndSetMaxVersion(); } }); this.providermappingComboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { retrieveCollections(); retrieveVersionsAndSetMaxVersion(); } }); this.collectionmappingComboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { retrieveVersionsAndSetMaxVersion(); } }); return builder.getResult(); } private JPanel createSelectTransformersRadioGroup() { this.localTransformersRadioButton = LocalizedUI.createRadioButton("Transform.UseLocalScripts"); //$NON-NLS-1$ this.localTransformersRadioButton.setSelected(true); this.remoteTransformersRadioButton = LocalizedUI.createRadioButton("Transform.UseRemoteScripts"); //$NON-NLS-1$ // You've to remove this if the transformer scripts presenting server is enabled. this.remoteTransformersRadioButton.setEnabled(false); // Group the radio buttons. ButtonGroup group = new ButtonGroup(); group.add(this.localTransformersRadioButton); group.add(this.remoteTransformersRadioButton); JPanel tranformersInnerPanel = new JPanel(new GridLayout(2, 1)); this.localTransformersRadioButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (TransformersPanel.this.localTransformersRadioButton.isSelected()) { TransformersPanel.this.myCortexModel.getConfig().put(AscConfiguration.TRANSFORMERS_URL, DEFAULT_TRANSFORMER_URL); } } }); this.remoteTransformersRadioButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (TransformersPanel.this.remoteTransformersRadioButton.isSelected()) { // get the frame the panel is embedded in Component currentComponent = TransformersPanel.this; while (currentComponent.getParent() != null && !(currentComponent instanceof JFrame)) { currentComponent = currentComponent.getParent(); } final JFrame parentFrame = currentComponent instanceof JFrame ? (JFrame) currentComponent : null; (new TransformationURLWindow(parentFrame, TransformersPanel.this.myCortexModel.getConfig())) .setVisible(true); if (TransformersPanel.this.myCortexModel.getConfig() .get(AscConfiguration.TRANSFORMERS_URL, DEFAULT_TRANSFORMER_URL) .equals(DEFAULT_TRANSFORMER_URL)) { TransformersPanel.this.remoteTransformersRadioButton.setSelected(false); TransformersPanel.this.localTransformersRadioButton.setSelected(true); } } } }); tranformersInnerPanel.add(this.localTransformersRadioButton); tranformersInnerPanel.add(this.remoteTransformersRadioButton); return tranformersInnerPanel; } // ----------------------------------------------------------------------- // // Providing transformers' selections // // ----------------------------------------------------------------------- /** * Providing transformers' selections */ private void retrieveVersionsAndSetMaxVersion() { this.versionComboBox.removeAllItems(); // retrieve versions ArrayList<String> sel = new ArrayList<String>(); sel.add((String) this.sourceMetadataComboBox.getSelectedItem()); sel.add((String) this.domainComboBox.getSelectedItem()); sel.add((String) this.providermappingComboBox.getSelectedItem()); sel.add((String) this.collectionmappingComboBox.getSelectedItem()); int maxvalue = 0; for (String value : sel) { if (value == null || value.equals("--")) { break; } maxvalue++; } if (maxvalue == 0) { return; } SipMakerKey description = SipMakerKey.fromTrunk(sel.get(0), sel.subList(1, maxvalue)); String[] versions = this.sipMakerFactory.getVersions(description); // put the retrieved versions in the combobox for (String version : versions) { this.versionComboBox.addItem(version); } // select max version int max = -1; int indexToSelect = -1; for (int i = 0; i < this.versionComboBox.getModel().getSize(); i++) { try { Integer version = Integer.valueOf(this.versionComboBox.getModel().getElementAt(i).toString()); if (version > max) { max = version; indexToSelect = i; } } catch (NumberFormatException ex) { // ignore } // something found if (max > -1 && indexToSelect > -1) { this.versionComboBox.setSelectedIndex(indexToSelect); } } } /** * retrieving the domain part */ private void retrieveDomains() { String metadataformat = (String) this.sourceMetadataComboBox.getSelectedItem(); SipMakerKey description = SipMakerKey.fromTrunk(metadataformat); String[] domains = this.sipMakerFactory.getSubPathValues(description); this.domainComboBox.removeAllItems(); this.domainComboBox.addItem("--"); //$NON-NLS-1$ for (String domain : domains) { this.domainComboBox.addItem(domain); } this.domainComboBox.setSelectedIndex(0); this.providermappingComboBox.removeAllItems(); this.providermappingComboBox.addItem("--"); //$NON-NLS-1$ this.providermappingComboBox.setSelectedIndex(0); this.collectionmappingComboBox.removeAllItems(); this.collectionmappingComboBox.addItem("--"); //$NON-NLS-1$ this.collectionmappingComboBox.setSelectedIndex(0); } /** * retrieving the provider part */ private void retrieveProviders() { String metadataformat = (String) this.sourceMetadataComboBox.getSelectedItem(); String domain = (String) this.domainComboBox.getSelectedItem(); SipMakerKey description = SipMakerKey.fromTrunk(metadataformat, domain); String[] providers = this.sipMakerFactory.getSubPathValues(description); this.providermappingComboBox.removeAllItems(); this.providermappingComboBox.addItem("--"); //$NON-NLS-1$ for (String provider : providers) { this.providermappingComboBox.addItem(provider); } this.providermappingComboBox.setSelectedIndex(0); this.collectionmappingComboBox.removeAllItems(); this.collectionmappingComboBox.addItem("--"); //$NON-NLS-1$ this.collectionmappingComboBox.setSelectedIndex(0); } /** * retrieving the collection part */ private void retrieveCollections() { String metadataformat = (String) this.sourceMetadataComboBox.getSelectedItem(); String domain = (String) this.domainComboBox.getSelectedItem(); String provider = (String) this.providermappingComboBox.getSelectedItem(); SipMakerKey description = SipMakerKey.fromTrunk(metadataformat, domain, provider); this.collectionmappingComboBox.removeAllItems(); this.collectionmappingComboBox.addItem("--"); //$NON-NLS-1$ for (String collection : this.sipMakerFactory.getSubPathValues(description)) { this.collectionmappingComboBox.addItem(collection); } this.collectionmappingComboBox.setSelectedIndex(0); } /** * update the UI with the context values */ @Override public void transferConfigurationToUI(AscConfiguration config) { // Metadata format String[] splittedmdformat = config.get(FORMAT, "").split(","); //$NON-NLS-1$ //$NON-NLS-2$ if (splittedmdformat.length >= 1) { if (!setSelectedString(this.sourceMetadataComboBox, splittedmdformat[0])) { LOG.error("metadataformat unknown."); //$NON-NLS-1$ } retrieveDomains(); } if (setSelectedString(this.domainComboBox, convertMDFormatPart(splittedmdformat, 1))) { retrieveProviders(); } else { LOG.error("domain unknown."); //$NON-NLS-1$ } if (setSelectedString(this.providermappingComboBox, convertMDFormatPart(splittedmdformat, 2))) { retrieveCollections(); } else { LOG.error("provider mapping unknown."); //$NON-NLS-1$ } if (!setSelectedString(this.providermappingComboBox, convertMDFormatPart(splittedmdformat, 3))) { LOG.error("collection mapping unknown."); //$NON-NLS-1$ } retrieveVersionsAndSetMaxVersion(); if (splittedmdformat.length >= 5) { String toSelect = splittedmdformat[4].trim(); if (!toSelect.isEmpty() && !toSelect.equals("max")) { if (!setSelectedString(this.versionComboBox, toSelect)) { LOG.error("version not found."); //$NON-NLS-1$ } } } if (config.get(AscConfiguration.TRANSFORMERS_URL, DEFAULT_TRANSFORMER_URL) .equals(DEFAULT_TRANSFORMER_URL)) { this.localTransformersRadioButton.setSelected(true); this.remoteTransformersRadioButton.setSelected(false); } else { this.localTransformersRadioButton.setSelected(false); this.remoteTransformersRadioButton.setSelected(true); } } /** * helper */ private boolean setSelectedString(JComboBox comboBox, String stringToSelect) { final ComboBoxModel model = comboBox.getModel(); int size = model.getSize(); for (int index = 0; index < size; index++) { if (model.getElementAt(index).toString().equals(stringToSelect)) { comboBox.setSelectedIndex(index); return true; } } return false; } private String convertMDFormatPart(String[] metadataFormatParts, int index) { String part = (index < metadataFormatParts.length) ? metadataFormatParts[index].trim() : ""; return part.isEmpty() ? "--" : part; } @Override public void transferUIToConfiguration(AscConfiguration config) { String mdformatstring = generateMetadataFormatProcessingString(); config.put(FORMAT, mdformatstring); } /** * helper method for transfering the metadatamapping information to the context. */ private String generateMetadataFormatProcessingString() { StringBuilder builder = new StringBuilder( ObjectUtils.toString(this.sourceMetadataComboBox.getSelectedItem())); builder.append(","); appendMetadataFormatPart(builder, this.domainComboBox); builder.append(","); appendMetadataFormatPart(builder, this.providermappingComboBox); builder.append(","); appendMetadataFormatPart(builder, this.collectionmappingComboBox); builder.append(","); builder.append(ObjectUtils.toString(this.versionComboBox.getSelectedItem())); String mdformatstring = builder.toString(); LOG.debug("Generated Metadataformat-String is: md_format = " + mdformatstring); //$NON-NLS-1$ return mdformatstring; } private void appendMetadataFormatPart(StringBuilder builder, JComboBox comboBox) { Object selectedItem = comboBox.getSelectedItem(); if (selectedItem == null) { return; } String partName = selectedItem.toString(); if (!partName.equals("--")) { builder.append(partName); } } @Override public final void update(Observable o, Object arg) { if (o instanceof MyCortexModel) { MyCortexModel myCortexModel = (MyCortexModel) o; this.sourceMetadataComboBox.setEnabled(!myCortexModel.isIngestRunning()); this.domainComboBox.setEnabled(!myCortexModel.isIngestRunning()); this.providermappingComboBox.setEnabled(!myCortexModel.isIngestRunning()); this.collectionmappingComboBox.setEnabled(!myCortexModel.isIngestRunning()); this.versionComboBox.setEnabled(!myCortexModel.isIngestRunning()); this.localTransformersRadioButton.setEnabled(!myCortexModel.isIngestRunning()); // You've to remove this if the transformer scripts presenting server is enabled. // this.remoteTransformersRadioButton.setEnabled(!myCortexModel.isIngestRunning()); } } }