/*
* Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of its contributors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package kameleon.gui.view;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import kameleon.exception.FileReadingException;
import kameleon.exception.KameleonException;
import kameleon.gui.exception.UnknownKeyException;
import kameleon.gui.language.SwitchLanguage;
import kameleon.gui.model.Model;
import kameleon.gui.util.FileConstants;
import kameleon.gui.util.LanguageConstants;
import kameleon.plugin.PlugInInfo;
/**
* Frame used by the user to select the analyzer plug-ins that should
* be deleted.
*
* @author Fromentin Xavier, Schnell Michaël
* @version 1.0
*/
public class DeleteAnalyzerFrame extends JDialog
implements LanguageConstants, FileConstants {
/**
* Needed to serialize this class.
*
* @see java.io.Serializable
*/
private static final long serialVersionUID = -4528708129605211397L ;
/**
* Model of this view.
*/
protected Model model ;
/**
* List of the selected analyzers (the selected analyzers are the
* ones that should be deleted).
*/
protected List<PlugInInfo> selectedAnalyzers ;
/**
* Delete button.
*/
protected JButton delete ;
/**
* Sole constructor.
*
* @param container
* parent frame of this instance
*
* @param model
* model of this view
*
* @throws KameleonException
* if an error occurred while build this instance
*/
public DeleteAnalyzerFrame(JFrame container, Model model)
throws KameleonException {
super(container, true) ;
this.model = model ;
this.selectedAnalyzers = new ArrayList<PlugInInfo>(
this.model.getKnownAnalyzers().size()) ;
this.setTitle(SwitchLanguage.getInstance()
.getText(DELETE_ANALYZER_MENU)) ;
this.build() ;
this.pack() ;
this.setResizable(false) ;
this.setLocationRelativeTo(container) ;
}// DeleteAnalyzerFrame(JFrame, Model)
/**
* Builds the content of this frame.
*
* @throws UnknownKeyException
* if a key for a displayed text could not be found
*/
private void build() throws UnknownKeyException {
GridBagLayout gridbag = new GridBagLayout() ;
this.setLayout(gridbag) ;
this.buildTitle(gridbag) ;
this.buildAnalyzers(gridbag) ;
this.buildButton(gridbag) ;
}// build()
/**
* Builds the introduction sentence displayed in this frame.
*
* @param gridbag
* layout manager used by this frame
*
* @throws UnknownKeyException
* if the key for the explanation text was not found
*/
private void buildTitle(GridBagLayout gridbag)
throws UnknownKeyException {
SwitchLanguage sl = SwitchLanguage.getInstance() ;
GridBagConstraints constraints ;
constraints = new GridBagConstraints() ;
constraints.gridwidth = GridBagConstraints.REMAINDER ;
constraints.anchor = GridBagConstraints.BASELINE ;
constraints.weightx = 1.0 ;
constraints.weighty = 0.0 ;
constraints.insets = new Insets(5, 5, 5, 5) ;
JLabel label = new JLabel(
sl.getText(DELETE_ANALYZER_EXPLANATION)) ;
this.add(label) ;
gridbag.setConstraints(label, constraints) ;
}// buildTitle(GridBagLayout)
/**
* Builds the entries for the analyzers, one per analyzer.
* Entries are displayed using the {@link ViewPlugInInfo} class.
*
* @param gridbag
* layout manager used by this frame
*
* @throws UnknownKeyException
* if the key for the explanation text was not found
*/
private void buildAnalyzers(GridBagLayout gridbag) throws UnknownKeyException {
GridBagConstraints checkBoxConstraints, plugInCnstraints ;
checkBoxConstraints = new GridBagConstraints() ;
checkBoxConstraints.gridwidth = 1 ;
checkBoxConstraints.anchor = GridBagConstraints.BASELINE_TRAILING ;
checkBoxConstraints.weightx = 0.0 ;
checkBoxConstraints.insets = new Insets(5, 50, 5, 5) ;
plugInCnstraints = new GridBagConstraints() ;
plugInCnstraints.gridwidth = GridBagConstraints.REMAINDER ;
plugInCnstraints.anchor = GridBagConstraints.BASELINE_LEADING ;
plugInCnstraints.weightx = 1.0 ;
plugInCnstraints.insets = new Insets(5, 5, 5, 50) ;
for(PlugInInfo analyzer : this.model.getKnownAnalyzers()) {
try {
final ViewPlugInInfo vpii =
new ViewPlugInInfo(this.model, analyzer, true) ;
final JCheckBox checkBox = new JCheckBox() ;
checkBox.setSelected(false) ;
checkBox.addItemListener(
new CheckBoxListener(this, vpii, analyzer)) ;
this.add(checkBox) ;
gridbag.setConstraints(checkBox, checkBoxConstraints) ;
this.add(vpii) ;
gridbag.setConstraints(vpii, plugInCnstraints) ;
} catch (FileReadingException fre) {
this.model.displayDebugInformation(fre) ;
}// try
}// for
}// buildAnalyzers(GridBagLayout)
/**
* Builds the delete button.
*
* @param gridbag
* layout manager used by this frame
*
* @throws UnknownKeyException
* if the key for the delete button text was not found
*/
private void buildButton(GridBagLayout gridbag)
throws UnknownKeyException {
SwitchLanguage sl = SwitchLanguage.getInstance() ;
GridBagConstraints constraints ;
constraints = new GridBagConstraints() ;
constraints.gridwidth = GridBagConstraints.REMAINDER ;
constraints.anchor = GridBagConstraints.BASELINE ;
constraints.weightx = 1.0 ;
constraints.weighty = 0.0 ;
constraints.insets = new Insets(5, 5, 5, 5) ;
this.delete = new JButton(sl.getText(DELETE_BUTTON)) ;
this.delete.setEnabled(false) ;
this.delete.addActionListener(new DeleteListener(this)) ;
this.add(this.delete) ;
gridbag.setConstraints(this.delete, constraints) ;
}// buildButton((GridBagLayout)
/**
* Add the given analyzer to the list of selected analyzers.
* If the analyzer is already in the list, nothing is done.
* Activates the delete button if necessary.
*
* @param analyzer
* analyzer which will be added to the list
*/
protected void addAnalyzer(PlugInInfo analyzer) {
if (!this.selectedAnalyzers.contains(analyzer)) {
this.selectedAnalyzers.add(analyzer) ;
}// if
this.delete.setEnabled(true) ;
}// addAnalyzer(PlugInInfo)
/**
* Removes the given analyzer from the list of selected analyzers.
* If the analyzer is not in the list, nothing is done.
* Deactivates the delete button is necessary.
*
* @param analyzer
* analyzer which will be removed from the list
*/
protected void removeAnalyzer(PlugInInfo analyzer) {
this.selectedAnalyzers.remove(analyzer) ;
this.delete.setEnabled(!this.selectedAnalyzers.isEmpty()) ;
}// removeAnalyzer(PlugInInfo)
/**
* Returns the model of this view.
*
* @return Model of this view
*/
protected Model getModel() {
return this.model ;
}// getModel()
/**
* Listener for the state changes of the check boxes of this frame.
* Updates the list of selected analyzers accordingly.
*
* @author Schnell Michaël
* @version 1.0
*/
private class CheckBoxListener implements ItemListener {
/**
* Parent frame of the component attached to this listener.
*/
protected DeleteAnalyzerFrame parent ;
/**
* Component displaying the handled plug-in.
*/
protected ViewPlugInInfo view ;
/**
* {@code PlugInInfo} handled by this listener.
*/
protected PlugInInfo info ;
/**
* Sole constructor.
*
* @param parent
* parent frame of the component attached to this
* listener
*
* @param view
* component displaying the handled plug-in
*
* @param info
* {@code PlugInInfo} handled by this listener
*/
public CheckBoxListener(DeleteAnalyzerFrame parent,
ViewPlugInInfo view, PlugInInfo info) {
super();
this.parent = parent ;
this.view = view ;
this.info = info ;
}// CheckBoxListener(DeleteAnalyzerFrame, ViewPlugInInfo, PlugInInfo)
/**
* Updates the list of selected analyzers (add a new analyzer
* of the check box is selected and removes it otherwise).
*
* @param e
* event fetched by the listener
*/
@Override
public void itemStateChanged(ItemEvent e) {
try {
if (e.getStateChange() == ItemEvent.SELECTED) {
this.parent.addAnalyzer(this.info) ;
this.view.setIconIsGrayed(false) ;
} else {
this.parent.removeAnalyzer(this.info) ;
this.view.setIconIsGrayed(true) ;
}// if
} catch (KameleonException ke) {
this.parent.getModel().displayDebugInformation(ke) ;
}// try
}// itemStateChange(ActionEvent)
}// class CheckBoxListener
/**
* Listener for clicks on the delete button of this frame.
* Transfers the list of the selected analyzers to model
* for deletion.
*
* @author Schnell Michaël
* @version 1.0
*/
private class DeleteListener implements ActionListener {
/**
* Parent frame of the component attached to this listener.
*/
protected DeleteAnalyzerFrame parent ;
/**
* /**
* Sole constructor.
*
* @param parent
* parent frame of the component attached to this
* listener
*/
public DeleteListener(DeleteAnalyzerFrame parent) {
super() ;
this.parent = parent ;
}// DeleteListener(DeleteAnalyzerFrame)
/**
* Removes the selected analyzers (without any confirmation).
* The parent frame is discarded at the end.
*
* @param e
* event fetched by the listener
*/
@Override
public void actionPerformed(ActionEvent e) {
for(PlugInInfo analyzer : this.parent.selectedAnalyzers) {
this.parent.model.removeAnalyzer(analyzer.getId()) ;
}// for
this.parent.dispose() ;
}// actionPerformed(ActionEvent)
}// class DeleteListener
}// class DeleteAnalyzerFrame