Java tutorial
package eu.apenet.dpt.standalone.gui.eag2012; /* * #%L * Data Preparation Tool Standalone mapping tool * %% * Copyright (C) 2009 - 2014 Archives Portal Europe * %% * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * * http://ec.europa.eu/idabc/eupl5 * * Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and limitations under the Licence. * #L% */ import java.awt.KeyboardFocusManager; import java.awt.event.ActionEvent; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.ResourceBundle; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.JTextField; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import org.apache.commons.lang.StringUtils; import com.jgoodies.forms.builder.PanelBuilder; import com.jgoodies.forms.layout.CellConstraints; import com.jgoodies.forms.layout.FormLayout; import eu.apenet.dpt.standalone.gui.ProfileListModel; import eu.apenet.dpt.standalone.gui.Utilities; import eu.apenet.dpt.standalone.gui.commons.ButtonTab; import eu.apenet.dpt.standalone.gui.commons.DefaultBtnAction; import eu.apenet.dpt.standalone.gui.commons.swingstructures.LanguageWithScript; import eu.apenet.dpt.standalone.gui.commons.swingstructures.TextFieldWithLanguage; import eu.apenet.dpt.standalone.gui.listener.FocusManagerListener; import eu.apenet.dpt.utils.eag2012.Abbreviation; import eu.apenet.dpt.utils.eag2012.Citation; import eu.apenet.dpt.utils.eag2012.ConventionDeclaration; import eu.apenet.dpt.utils.eag2012.Eag; import eu.apenet.dpt.utils.eag2012.Language; import eu.apenet.dpt.utils.eag2012.LanguageDeclaration; import eu.apenet.dpt.utils.eag2012.LanguageDeclarations; import eu.apenet.dpt.utils.eag2012.MaintenanceEvent; import eu.apenet.dpt.utils.eag2012.Script; import eu.apenet.dpt.utils.util.LanguageIsoList; /** * User: Yoann Moranville * Date: 28/11/2012 * * @author Yoann Moranville */ public class EagControlPanel extends EagPanels { private List<LanguageWithScript> languageWithScriptTfs; private List<TextFieldWithLanguage> rulesConventionTfs; private TextFieldWithLanguage personInstitutionRespTf; public EagControlPanel(Eag eag, JTabbedPane tabbedPane, JTabbedPane mainTabbedPane, JFrame eag2012Frame, ProfileListModel model, ResourceBundle labels, int repositoryNb) { super(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb); } @Override protected JComponent buildEditorPanel(List<String> errors) { if (errors == null) errors = new ArrayList<String>(0); else if (Utilities.isDev && errors.size() > 0) { LOG.info("Errors in form:"); for (String error : errors) { LOG.info(error); } } FormLayout layout = new FormLayout("right:max(50dlu;p), 4dlu, 100dlu, 7dlu, right:p, 4dlu, 100dlu", EDITOR_ROW_SPEC); layout.setColumnGroups(new int[][] { { 1, 3, 5, 7 } }); PanelBuilder builder = new PanelBuilder(layout); builder.setDefaultDialogBorder(); CellConstraints cc = new CellConstraints(); rowNb = 1; builder.addLabel(labels.getString("eag2012.commons.idUsedInApe") + "*", cc.xy(1, rowNb)); JTextField recordIdTf = new JTextField(eag.getControl().getRecordId().getValue()); recordIdTf.setEnabled(false); builder.add(recordIdTf, cc.xy(3, rowNb)); setNextRow(); builder.addLabel(labels.getString("eag2012.commons.personResponsible"), cc.xy(1, rowNb)); int sizeEvents = eag.getControl().getMaintenanceHistory().getMaintenanceEvent().size(); if (sizeEvents > 0) { MaintenanceEvent event = eag.getControl().getMaintenanceHistory().getMaintenanceEvent() .get(sizeEvents - 1); personInstitutionRespTf = new TextFieldWithLanguage(event.getAgent().getContent(), event.getAgent().getLang()); } else { personInstitutionRespTf = new TextFieldWithLanguage("", ""); } personInstitutionRespTf.getTextField().setEnabled(false); builder.add(personInstitutionRespTf.getTextField(), cc.xy(3, rowNb)); builder.addLabel(labels.getString("eag2012.commons.language"), cc.xy(5, rowNb)); builder.add(personInstitutionRespTf.getLanguageBox(), cc.xy(7, rowNb)); setNextRow(); builder.addSeparator(labels.getString("eag2012.control.usedLanguages"), cc.xyw(1, rowNb, 7)); setNextRow(); if (eag.getControl().getLanguageDeclarations() == null) eag.getControl().setLanguageDeclarations(new LanguageDeclarations()); if (eag.getControl().getLanguageDeclarations().getLanguageDeclaration().size() == 0) { LanguageDeclaration languageDeclaration = new LanguageDeclaration(); languageDeclaration.setLanguage(new Language()); languageDeclaration.setScript(new Script()); eag.getControl().getLanguageDeclarations().getLanguageDeclaration().add(languageDeclaration); } int i = 0; languageWithScriptTfs = new ArrayList<LanguageWithScript>( eag.getControl().getLanguageDeclarations().getLanguageDeclaration().size()); for (LanguageDeclaration languageDeclaration : eag.getControl().getLanguageDeclarations() .getLanguageDeclaration()) { builder.addLabel(labels.getString("eag2012.commons.language"), cc.xy(1, rowNb)); LanguageWithScript languageWithScript = new LanguageWithScript( languageDeclaration.getLanguage().getLanguageCode(), languageDeclaration.getScript().getScriptCode(), labels); languageWithScriptTfs.add(languageWithScript); builder.add(languageWithScript.getLanguageBox(), cc.xy(3, rowNb)); builder.addLabel(labels.getString("eag2012.control.descriptionScript"), cc.xy(5, rowNb)); builder.add(languageWithScript.getScriptBox(), cc.xy(7, rowNb)); setNextRow(); } if (errors.contains("languageWithScriptTfs")) { builder.add(createErrorLabel(labels.getString("eag2012.errors.noscript")), cc.xy(3, rowNb)); } JButton addLanguagesBtn = new ButtonTab(labels.getString("eag2012.control.addFurtherLangsAnsScripts")); builder.add(addLanguagesBtn, cc.xy(5, rowNb)); addLanguagesBtn.addActionListener(new AddLanguagesBtnAction(eag, tabbedPane, model)); setNextRow(); builder.addSeparator(labels.getString("eag2012.control.conventions"), cc.xyw(1, rowNb, 7)); setNextRow(); if (eag.getControl().getConventionDeclaration().size() == 0) eag.getControl().getConventionDeclaration().add(new ConventionDeclaration()); rulesConventionTfs = new ArrayList<TextFieldWithLanguage>( eag.getControl().getConventionDeclaration().size()); for (ConventionDeclaration conventionDeclaration : eag.getControl().getConventionDeclaration()) { builder.addLabel(labels.getString("eag2012.control.abbreviation"), cc.xy(1, rowNb)); TextFieldWithLanguage textFieldWithLanguage = new TextFieldWithLanguage( conventionDeclaration.getAbbreviation().getContent(), "", conventionDeclaration.getCitation().get(0).getContent()); rulesConventionTfs.add(textFieldWithLanguage); builder.add(textFieldWithLanguage.getTextField(), cc.xy(3, rowNb)); builder.addLabel(labels.getString("eag2012.control.fullName"), cc.xy(5, rowNb)); builder.add(textFieldWithLanguage.getExtraField(), cc.xy(7, rowNb)); setNextRow(); } JButton addConventionBtn = new ButtonTab(labels.getString("eag2012.commons.addCnventions")); builder.add(addConventionBtn, cc.xy(5, rowNb)); addConventionBtn.addActionListener(new AddConventionBtnAction(eag, tabbedPane, model)); setNextRow(); builder.addSeparator("", cc.xyw(1, rowNb, 7)); setNextRow(); JButton exitBtn = new ButtonTab(labels.getString("eag2012.commons.exit")); builder.add(exitBtn, cc.xy(1, rowNb)); exitBtn.addActionListener(new ExitBtnAction(eag, tabbedPane, model)); JButton previousTabBtn = new ButtonTab(labels.getString("eag2012.commons.previousTab")); builder.add(previousTabBtn, cc.xy(3, rowNb)); previousTabBtn.addActionListener(new ChangeTabBtnAction(eag, tabbedPane, model, false)); JButton nextTabBtn = new ButtonTab(labels.getString("eag2012.commons.nextTab")); builder.add(nextTabBtn, cc.xy(5, rowNb)); nextTabBtn.addActionListener(new ChangeTabBtnAction(eag, tabbedPane, model, true)); setNextRow(); JButton saveBtn = new ButtonTab(labels.getString("eag2012.commons.save")); builder.add(saveBtn, cc.xy(5, rowNb)); saveBtn.addActionListener(new SaveBtnAction(eag, tabbedPane, model)); setNextRow(); builder.addSeparator("", cc.xyw(1, rowNb, 7)); setNextRow(); JButton nextInstitutionTabBtn = new ButtonTab(labels.getString("eag2012.controls.nextInstitution")); nextInstitutionTabBtn.addActionListener(new NextInstitutionTabBtnAction(eag, tabbedPane, model)); builder.add(nextInstitutionTabBtn, cc.xy(5, rowNb)); // Define the change tab listener. this.removeChangeListener(); this.tabbedPane.addChangeListener(new ChangeTabListener(this.eag, this.tabbedPane, this.model, 5)); JPanel panel = builder.getPanel(); KeyboardFocusManager.getCurrentKeyboardFocusManager() .addPropertyChangeListener(new FocusManagerListener(panel)); return panel; } /** * Method that removes the existing "ChangeTabListener". */ private void removeChangeListener() { // Check the current "ChangeListeners" and remove the non desired ones. ChangeListener[] changeListeners = this.tabbedPane.getChangeListeners(); List<ChangeListener> changeListenerList = new LinkedList<ChangeListener>(); for (int i = 0; i < changeListeners.length; i++) { ChangeListener changeListener = changeListeners[i]; if (changeListener instanceof ChangeTabListener) { changeListenerList.add(changeListener); } } if (changeListenerList != null) { for (int i = 0; i < changeListenerList.size(); i++) { this.tabbedPane.removeChangeListener(changeListenerList.get(i)); } } } public class NextInstitutionTabBtnAction extends UpdateEagObject { NextInstitutionTabBtnAction(Eag eag, JTabbedPane tabbedPane, ProfileListModel model) { super(eag, tabbedPane, model); } @Override public void actionPerformed(ActionEvent actionEvent) { try { super.updateJAXBObject(true); int currentTabIndex = mainTabbedPane.getSelectedIndex(); if (currentTabIndex + 1 < mainTabbedPane.getTabCount()) { reloadTabbedPanel(new EagControlPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 5); mainTabbedPane.setEnabledAt(currentTabIndex, false); mainTabbedPane.setEnabledAt(currentTabIndex + 1, true); mainTabbedPane.setSelectedIndex(currentTabIndex + 1); } } catch (Eag2012FormException e) { reloadTabbedPanel(new EagControlPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 5); } } } public class AddLanguagesBtnAction extends UpdateEagObject { AddLanguagesBtnAction(Eag eag, JTabbedPane tabbedPane, ProfileListModel model) { super(eag, tabbedPane, model); } @Override public void actionPerformed(ActionEvent actionEvent) { try { super.updateJAXBObject(false); } catch (Eag2012FormException e) { } boolean empty = false; for (int i = 0; !empty && i < languageWithScriptTfs.size(); i++) { if (StringUtils.isEmpty(languageWithScriptTfs.get(i).getScript()) || languageWithScriptTfs.get(i).getLanguage() == null) empty = true; } if (empty) JOptionPane.showMessageDialog(eag2012Frame, labels.getString("eag2012.errors.addFurtherLangsAnsScripts")); LanguageDeclaration languageDeclaration = new LanguageDeclaration(); languageDeclaration.setLanguage(new Language()); languageDeclaration.setScript(new Script()); if (eag.getControl().getLanguageDeclarations() == null) eag.getControl().setLanguageDeclarations(new LanguageDeclarations()); eag.getControl().getLanguageDeclarations().getLanguageDeclaration().add(languageDeclaration); reloadTabbedPanel( new EagControlPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb) .buildEditorPanel(errors), 5); } } public class AddConventionBtnAction extends UpdateEagObject { AddConventionBtnAction(Eag eag, JTabbedPane tabbedPane, ProfileListModel model) { super(eag, tabbedPane, model); } @Override public void actionPerformed(ActionEvent actionEvent) { try { super.updateJAXBObject(false); } catch (Eag2012FormException e) { } boolean empty = false; for (int i = 0; !empty && i < rulesConventionTfs.size(); i++) { if (rulesConventionTfs.get(i).getTextValue() == null || rulesConventionTfs.get(i).getTextValue().trim().compareTo("") == 0 || rulesConventionTfs.get(i).getExtraValue() == null || rulesConventionTfs.get(i).getExtraValue().trim().compareTo("") == 0) empty = true; } if (empty) JOptionPane.showMessageDialog(eag2012Frame, labels.getString("eag2012.errors.addCnventions")); ConventionDeclaration conventionDeclaration = new ConventionDeclaration(); conventionDeclaration.setAbbreviation(new Abbreviation()); conventionDeclaration.getCitation().add(new Citation()); eag.getControl().getConventionDeclaration().add(conventionDeclaration); reloadTabbedPanel( new EagControlPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb) .buildEditorPanel(errors), 5); } } public class SaveBtnAction extends UpdateEagObject { SaveBtnAction(Eag eag, JTabbedPane tabbedPane, ProfileListModel model) { super(eag, tabbedPane, model); } @Override public void actionPerformed(ActionEvent actionEvent) { try { super.updateJAXBObject(true); super.saveFile(eag.getControl().getRecordId().getValue()); reloadTabbedPanel(new EagControlPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 5); } catch (Eag2012FormException e) { reloadTabbedPanel(new EagControlPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 5); } } } public class ChangeTabBtnAction extends UpdateEagObject { private boolean isNextTab; ChangeTabBtnAction(Eag eag, JTabbedPane tabbedPane, ProfileListModel model, boolean isNextTab) { super(eag, tabbedPane, model); this.isNextTab = isNextTab; } @Override public void actionPerformed(ActionEvent actionEvent) { try { super.updateJAXBObject(false); removeChangeListener(); if (isNextTab) { reloadTabbedPanel(new EagRelationsPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 6); } else { reloadTabbedPanel(new EagDescriptionPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 4); } } catch (Eag2012FormException e) { reloadTabbedPanel(new EagControlPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 5); } } } public abstract class UpdateEagObject extends DefaultBtnAction { public UpdateEagObject(Eag eag, JTabbedPane tabbedPane, ProfileListModel model) { super(eag, tabbedPane, model); } @Override protected void updateJAXBObject(boolean save) throws Eag2012FormException { errors = new ArrayList<String>(); int sizeEvents = eag.getControl().getMaintenanceHistory().getMaintenanceEvent().size(); if (sizeEvents > 0) { MaintenanceEvent event = eag.getControl().getMaintenanceHistory().getMaintenanceEvent() .get(sizeEvents - 1); event.getAgent().setLang(personInstitutionRespTf.getLanguage()); } if (languageWithScriptTfs.size() < 2 && StringUtils.isEmpty(languageWithScriptTfs.get(0).getLanguage())) { eag.getControl().setLanguageDeclarations(null); } else { eag.getControl().getLanguageDeclarations().getLanguageDeclaration().clear(); for (LanguageWithScript languageWithScript : languageWithScriptTfs) { if (StringUtils.isNotEmpty(languageWithScript.getLanguage()) && StringUtils.isNotEmpty(languageWithScript.getScript())) { LanguageDeclaration languageDeclaration = new LanguageDeclaration(); Language language = new Language(); language.setLanguageCode(languageWithScript.getLanguage()); language.setContent(LanguageIsoList.getLanguageStr(languageWithScript.getLanguage())); Script script = new Script(); script.setScriptCode(languageWithScript.getScript()); script.setContent(languageWithScript.getScript()); languageDeclaration.setLanguage(language); languageDeclaration.setScript(script); eag.getControl().getLanguageDeclarations().getLanguageDeclaration() .add(languageDeclaration); } else if (StringUtils.isNotEmpty(languageWithScript.getLanguage())) { errors.add("languageWithScriptTfs"); } } } if (rulesConventionTfs.size() > 0) { eag.getControl().getConventionDeclaration().clear(); for (TextFieldWithLanguage textFieldWithLanguage : rulesConventionTfs) { if (StringUtils.isNotEmpty(textFieldWithLanguage.getTextValue())) { ConventionDeclaration conventionDeclaration = new ConventionDeclaration(); Abbreviation abbreviation = new Abbreviation(); abbreviation.setContent(textFieldWithLanguage.getTextValue()); Citation citation = new Citation(); citation.setContent(textFieldWithLanguage.getExtraValue()); conventionDeclaration.setAbbreviation(abbreviation); conventionDeclaration.getCitation().add(citation); eag.getControl().getConventionDeclaration().add(conventionDeclaration); } } } if (!errors.isEmpty()) { throw new Eag2012FormException("Errors in validation of EAG 2012"); } } } /** * Class to performs the action when the user clicks in the exit button */ protected class ExitBtnAction extends UpdateEagObject { /** * Constructor. * * @param eag2012 * @param tabbedPane * @param model */ public ExitBtnAction(Eag eag, JTabbedPane tabbedPane, ProfileListModel model) { super(eag, tabbedPane, model); } @Override public void actionPerformed(ActionEvent actionEvent) { int event = JOptionPane.showConfirmDialog(tabbedPane, labels.getString("eaccpf.commons.exitConfirm"), labels.getString("eag2012.eag2012Item"), JOptionPane.YES_NO_OPTION); try { if (event == JOptionPane.YES_OPTION) { super.updateJAXBObject(true); super.saveFile(eag.getControl().getRecordId().getValue()); closeFrame(); } else if (event == JOptionPane.NO_OPTION) { Eag2012Frame.inUse(false); closeFrame(); } } catch (Eag2012FormException e) { reloadTabbedPanel(new EagControlPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 5); } } } /** * Class to performs the actions when the user clicks in other tab. */ public class ChangeTabListener extends UpdateEagObject implements ChangeListener { private int currentTab; /** * Constructor. * * @param eaccpf * @param tabbedPane * @param model * @param indexTab */ public ChangeTabListener(Eag eag, JTabbedPane tabbedPane, ProfileListModel model, int indexTab) { super(eag, tabbedPane, model); this.currentTab = indexTab; } @Override public void stateChanged(ChangeEvent e) { int selectedIndex = this.tabbedPane.getSelectedIndex(); // Checks if clicks in different tab. if (this.currentTab != selectedIndex) { try { super.updateJAXBObject(true); removeChangeListener(); switch (selectedIndex) { case 0: reloadTabbedPanel(new EagInstitutionPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, false, labels, repositoryNb).buildEditorPanel(errors), 0); break; case 1: reloadTabbedPanel(new EagIdentityPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 1); break; case 2: reloadTabbedPanel(new EagContactPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, false, labels, repositoryNb).buildEditorPanel(errors), 2); break; case 3: reloadTabbedPanel(new EagAccessAndServicesPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 3); break; case 4: reloadTabbedPanel(new EagDescriptionPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 4); break; case 6: reloadTabbedPanel(new EagRelationsPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 6); break; default: reloadTabbedPanel(new EagControlPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 5); } } catch (Eag2012FormException ex) { reloadTabbedPanel(new EagControlPanel(eag, tabbedPane, mainTabbedPane, eag2012Frame, model, labels, repositoryNb).buildEditorPanel(errors), 5); } } } @Override public void actionPerformed(ActionEvent actionEvent) { // Empty. } } }