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 static de.fhg.iais.asc.commons.AscConfiguration.HARVESTING_FROM_DATE; import static de.fhg.iais.asc.commons.AscConfiguration.HARVESTING_UNTIL_DATE; import static de.fhg.iais.asc.commons.AscConfiguration.INBOX; import static de.fhg.iais.asc.commons.AscConfiguration.META_DATA_FORMAT; import static de.fhg.iais.asc.commons.AscConfiguration.OAI_SET; import static de.fhg.iais.asc.commons.AscConfiguration.OAI_SOURCE; import static de.fhg.iais.asc.commons.AscConfiguration.OUTBOX; import static de.fhg.iais.asc.commons.AscConfiguration.SOURCE_FOLDER; import static de.fhg.iais.asc.commons.AscConfiguration.STRATEGY; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Observable; import javax.swing.ComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; import de.fhg.iais.asc.commons.AscConfigEval; import de.fhg.iais.asc.commons.AscConfiguration; import de.fhg.iais.asc.oai.retriever.MetadataFormatsAndSetNamesRetriever; import de.fhg.iais.asc.ui.MyCortex; import de.fhg.iais.asc.ui.MyCortexModel; import de.fhg.iais.asc.ui.components.panel.LeftLabeledElementJPanel; import de.fhg.iais.asc.ui.components.textfield.DatePickerJTextField; import de.fhg.iais.asc.ui.i18n.LocalizedOptionPane; import de.fhg.iais.asc.ui.i18n.LocalizedUI; import de.fhg.iais.asc.ui.i18n.Messages; import de.fhg.iais.asc.ui.windows.SetSelectionWindow; import de.fhg.iais.commons.constants.Serial; public class HarvesterPanel extends LeftLabeledElementJPanel implements MyCortexUIComponent { private static final long serialVersionUID = Serial.GLOBAL_DEFAULT_SERIAL_VERSION_UID; private JTextField oaipmhTextField; private JButton retrieveInformationButton; private JComboBox oaipmhMetadataComboBox; private String[] availableSets; private JLabel oaipmhMetadataFormatLabel; private JTextField setsTextField; private JComboBox harvestingStrategyComboBox; private DatePickerJTextField fromDateTextField; private DatePickerJTextField untilDateTextField; private String inboxfolder; private String outboxfolder; private final MyCortexModel myCortexModel; private static final String[] HARVEST_STRATEGIES = { "multirecord", "chunk", "completeset" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ }; public HarvesterPanel(MyCortexModel myCortexModel) { super("Harvest"); //$NON-NLS-1$ init(myCortexModel); this.myCortexModel = myCortexModel; myCortexModel.addObserver(this); update(myCortexModel, null); } private void init(final MyCortexModel myCortexModel) { // OAI-PMH URL this.oaipmhTextField = new JTextField(20); this.retrieveInformationButton = LocalizedUI.createButton("Contact_Repository", new ActionListener() { @Override public void actionPerformed(ActionEvent e) { MetadataFormatsAndSetNamesRetriever retriever = new MetadataFormatsAndSetNamesRetriever( HarvesterPanel.this.oaipmhTextField.getText(), myCortexModel.getConfig().getASCState(), myCortexModel.getConfig().get(AscConfiguration.OAIPMH_PROXYHOST, ""), //$NON-NLS-1$ Integer.valueOf(myCortexModel.getConfig().get(AscConfiguration.OAIPMH_PROXYPORT, 0))); String[] metadataformats = retriever.listMetadataFormats(); HarvesterPanel.this.oaipmhMetadataComboBox.removeAllItems(); for (String metadataformat : metadataformats) { HarvesterPanel.this.oaipmhMetadataComboBox.addItem(metadataformat); } HarvesterPanel.this.availableSets = retriever.listSets(); if (metadataformats.length == 0 && HarvesterPanel.this.availableSets == null) { // get the frame the panel is embedded in Component currentComponent = HarvesterPanel.this; while (currentComponent.getParent() != null && !(currentComponent instanceof JFrame)) { currentComponent = currentComponent.getParent(); } final JFrame parentFrame = currentComponent instanceof JFrame ? (JFrame) currentComponent : null; LocalizedOptionPane.showMessageDialog(parentFrame, "UnableToContactRepository", JOptionPane.ERROR_MESSAGE); return; } } }); this.add("OAI-PMH_URL", this.oaipmhTextField, this.retrieveInformationButton); this.oaipmhMetadataComboBox = this.add("Metadata_format", new JComboBox()); this.oaipmhMetadataComboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Object item = HarvesterPanel.this.oaipmhMetadataComboBox.getSelectedItem(); myCortexModel.setSelectedMetadataFolder(ObjectUtils.toString(item, null)); } }); this.oaipmhMetadataFormatLabel = this.getLastLabel(); this.setsTextField = this.add("Sets", createSetsTextField()); this.harvestingStrategyComboBox = this.add("Harvesting_Strategy", new JComboBox(HARVEST_STRATEGIES)); this.harvestingStrategyComboBox.setSelectedIndex(0); this.fromDateTextField = this.add("From_date", new DatePickerJTextField()); this.untilDateTextField = this.add("Until_date", new DatePickerJTextField()); this.inboxfolder = myCortexModel.getConfig().get(INBOX, MyCortex.DEFAULT_REPOSITORY_LOCATION + "/asc/inbox"); //$NON-NLS-1$ this.outboxfolder = myCortexModel.getConfig().get(OUTBOX, MyCortex.DEFAULT_REPOSITORY_LOCATION + "/asc/outbox"); //$NON-NLS-1$ } private JTextField createSetsTextField() { final JTextField textField = new JTextField(30); textField.addFocusListener(new FocusAdapter() { @Override public void focusGained(FocusEvent e) { e.getOppositeComponent().requestFocus(); // get the frame the panel is embedded in Component currentComponent = HarvesterPanel.this; while (currentComponent.getParent() != null && !(currentComponent instanceof JFrame)) { currentComponent = currentComponent.getParent(); } final JFrame parentFrame = currentComponent instanceof JFrame ? (JFrame) currentComponent : null; if (HarvesterPanel.this.availableSets == null) { LocalizedOptionPane.showMessageDialog(parentFrame, "No_setnames_retrieved", JOptionPane.ERROR_MESSAGE); return; } String[] selectedSets = textField.getText().split(","); //$NON-NLS-1$ for (int i = 0; i < selectedSets.length; i++) { selectedSets[i] = selectedSets[i].trim(); } SetSelectionWindow ssw = new SetSelectionWindow(parentFrame, HarvesterPanel.this.availableSets, selectedSets); ssw.setVisible(true); textField.setText(StringUtils.join(ssw.getSelectedSets(), ", ")); } }); return textField; } @Override public final void update(Observable o, Object arg) { if (o instanceof MyCortexModel) { MyCortexModel ascuimodel = (MyCortexModel) o; this.oaipmhMetadataComboBox.setEnabled(!ascuimodel.isIngestRunning()); this.oaipmhTextField.setEditable(!ascuimodel.isIngestRunning()); this.setsTextField.setEditable(!ascuimodel.isIngestRunning()); this.harvestingStrategyComboBox .setEnabled(!ascuimodel.isIngestRunning() && ascuimodel.isHarvestingSelected()); updateAvailableFormatsOrFolders(ascuimodel); } } private String backupedHarvestingFormat = null; private String[] backupedHarvestingFormats = null; private String backupedTransformationSourceFolder = null; private String backupedSendingSourceFolder = null; private boolean wasHarvestingSelected; private boolean wasTransformationSelected; private boolean wasSendingSelected; private void updateAvailableFormatsOrFolders(MyCortexModel myCortexModel) { if (myCortexModel.isHarvestingSelected() == this.wasHarvestingSelected && myCortexModel.isTransformationSelected() == this.wasTransformationSelected && myCortexModel.isSendSelected() == this.wasSendingSelected) { // leave it as it is if nothing has been updated return; } // backing up String backupString = this.oaipmhMetadataComboBox.getSelectedItem() != null ? this.oaipmhMetadataComboBox.getSelectedItem().toString() : null; if (this.wasHarvestingSelected) { this.backupedHarvestingFormat = backupString; this.backupedHarvestingFormats = new String[this.oaipmhMetadataComboBox.getModel().getSize()]; for (int i = 0; i < this.oaipmhMetadataComboBox.getModel().getSize(); i++) { this.backupedHarvestingFormats[i] = this.oaipmhMetadataComboBox.getModel().getElementAt(i) .toString(); } } else if (this.wasTransformationSelected) { this.backupedTransformationSourceFolder = backupString; } else if (this.wasSendingSelected) { this.backupedSendingSourceFolder = backupString; } // update status this.wasHarvestingSelected = myCortexModel.isHarvestingSelected(); this.wasTransformationSelected = myCortexModel.isTransformationSelected(); this.wasSendingSelected = myCortexModel.isSendSelected(); if (myCortexModel.isHarvestingSelected()) { // -- change from non-harvesting to harvesting -- setUIToHarvesting(); } else if (myCortexModel.isTransformationSelected()) { // -- change from harvesting to non-harvesting -- setUIToFolder(this.inboxfolder, this.backupedTransformationSourceFolder); } else if (myCortexModel.isSendSelected()) { // -- change from harvesting to non-harvesting -- setUIToFolder(this.outboxfolder, this.backupedSendingSourceFolder); } else { // nothing is selected // remove everything from the list this.oaipmhMetadataComboBox.removeAllItems(); } } /** * Harvesting has been activated. Display the harvesting format in the list if possible and enable all * textfields. */ private void setUIToHarvesting() { // remove everything from the list this.oaipmhMetadataComboBox.removeAllItems(); // set saved harvesting format if (this.backupedHarvestingFormats != null) { int pos = -1; for (int i = 0; i < this.backupedHarvestingFormats.length; i++) { if (this.backupedHarvestingFormats[i].equals(this.backupedHarvestingFormat)) { pos = i; } this.oaipmhMetadataComboBox.addItem(this.backupedHarvestingFormats[i]); } if (pos >= 0) { this.oaipmhMetadataComboBox.setSelectedIndex(pos); } } // UI Updates this.oaipmhTextField.setEnabled(true); this.setsTextField.setEnabled(true); this.fromDateTextField.setEnabled(true); this.untilDateTextField.setEnabled(true); this.harvestingStrategyComboBox.setEnabled(true); this.retrieveInformationButton.setEnabled(true); this.oaipmhMetadataFormatLabel.setText(Messages.getString("Harvest.Label.Metadata_format")); //$NON-NLS-1$ } /** * Folder reading has been activated. Display available folders in the list and deactivate the textfields. * * @param folder * @param previousValue */ private void setUIToFolder(String folder, String previousValue) { // remove everything from the list this.oaipmhMetadataComboBox.removeAllItems(); // reading the file system directory structure to display directory // names for (String oneDir : retrieveFolderNamesFromFilesystem(new File(folder))) { this.oaipmhMetadataComboBox.addItem(oneDir); } // select the old source folder if possible if (previousValue != null) { setSelectedString(this.oaipmhMetadataComboBox, previousValue); } // UI Updates this.oaipmhTextField.setEnabled(false); this.setsTextField.setEnabled(false); this.fromDateTextField.setEnabled(false); this.untilDateTextField.setEnabled(false); this.harvestingStrategyComboBox.setEnabled(false); this.retrieveInformationButton.setEnabled(false); this.oaipmhMetadataFormatLabel.setText(Messages.getString("Harvest.Label.Incoming_folder")); //$NON-NLS-1$ } /** * loading the folders of a directory. * * @param dir * @return */ private String[] retrieveFolderNamesFromFilesystem(File dir) { File[] filesInDir = dir.listFiles(); if (filesInDir == null) { return new String[0]; } ArrayList<String> dirs = new ArrayList<String>(); for (File file : filesInDir) { if (file.isDirectory()) { dirs.add(file.getName()); } } String[] dirsArray = dirs.toArray(new String[dirs.size()]); // sort Arrays.sort(dirsArray); return dirsArray; } @Override public void transferConfigurationToUI(AscConfiguration config) { String oaipmhMetadata = ObjectUtils.toString(this.oaipmhMetadataComboBox.getSelectedItem()); config.put(SOURCE_FOLDER, oaipmhMetadata); if (this.myCortexModel.isHarvestingSelected()) { config.put(META_DATA_FORMAT, oaipmhMetadata); config.put(SOURCE_FOLDER, oaipmhMetadata); } setSelectedString(this.harvestingStrategyComboBox, config.get(STRATEGY, "multirecord")); //$NON-NLS-1$ this.oaipmhTextField.setText(config.get(OAI_SOURCE, "")); //$NON-NLS-1$ this.setsTextField.setText(config.get(OAI_SET, "")); //$NON-NLS-1$ this.fromDateTextField.setText(config.get(HARVESTING_FROM_DATE, "")); //$NON-NLS-1$ this.untilDateTextField.setText(config.get(HARVESTING_UNTIL_DATE, "")); //$NON-NLS-1$ boolean doHarvest = AscConfigEval.isHarvestEnabled(config); boolean doTransform = AscConfigEval.isTransformEnabled(config); boolean doSubmit = AscConfigEval.isSubmitEnabled(config); this.oaipmhMetadataComboBox.removeAllItems(); if (doHarvest) { String harvestingFormat = config.get(META_DATA_FORMAT, config.get(FORMAT, "")); //$NON-NLS-1$ if (!setSelectedString(this.oaipmhMetadataComboBox, harvestingFormat)) { this.oaipmhMetadataComboBox.addItem(harvestingFormat); setSelectedString(this.oaipmhMetadataComboBox, harvestingFormat); } this.backupedHarvestingFormat = harvestingFormat; } else { String folder = doTransform ? this.inboxfolder : this.outboxfolder; setUIToFolder(folder, null); String sourceFolder = config.get(SOURCE_FOLDER, config.get(FORMAT, "")); //$NON-NLS-1$ setSelectedString(this.oaipmhMetadataComboBox, sourceFolder); this.backupedTransformationSourceFolder = sourceFolder; } this.wasHarvestingSelected = doHarvest; this.wasTransformationSelected = doTransform; this.wasSendingSelected = doSubmit; } @Override public void transferUIToConfiguration(AscConfiguration config) { config.put(STRATEGY, this.harvestingStrategyComboBox.getSelectedItem().toString()); config.put(OAI_SOURCE, this.oaipmhTextField.getText()); String oaipmhMetadata = ObjectUtils.toString(this.oaipmhMetadataComboBox.getSelectedItem()); config.put(SOURCE_FOLDER, oaipmhMetadata); if (this.myCortexModel.isHarvestingSelected()) { config.put(META_DATA_FORMAT, oaipmhMetadata); } config.put(OAI_SET, this.setsTextField.getText()); config.put(HARVESTING_FROM_DATE, this.fromDateTextField.getText()); config.put(HARVESTING_UNTIL_DATE, this.untilDateTextField.getText()); } 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; } }