/*
* 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 generator plug-ins that should
* be deleted.
*
* @author Fromentin Xavier, Schnell Michaël
* @version 1.0
*/
public class DeleteGeneratorFrame 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 generators (the selected generators are the
* ones that should be deleted).
*/
protected List<PlugInInfo> selectedGenerators ;
/**
* 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 DeleteGeneratorFrame(JFrame container, Model model)
throws KameleonException {
super(container, true) ;
this.model = model ;
this.selectedGenerators = new ArrayList<PlugInInfo>(
this.model.getKnownGenerators().size()) ;
this.setTitle(SwitchLanguage.getInstance()
.getText(DELETE_GENERATOR_MENU)) ;
this.build() ;
this.pack() ;
this.setResizable(false) ;
this.setLocationRelativeTo(container) ;
}// DeleteGeneratorFrame(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.buildGenerators(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_GENERATOR_EXPLANATION)) ;
this.add(label) ;
gridbag.setConstraints(label, constraints) ;
}// buildTitle(GridBagLayout)
/**
* Builds the entries for the generators, one per generator.
* 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 buildGenerators(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 generator : this.model.getKnownGenerators()) {
try {
final ViewPlugInInfo vpii =
new ViewPlugInInfo(this.model, generator, true) ;
final JCheckBox checkBox = new JCheckBox() ;
checkBox.setSelected(false) ;
checkBox.addItemListener(
new CheckBoxListener(this, vpii, generator)) ;
this.add(checkBox) ;
gridbag.setConstraints(checkBox, checkBoxConstraints) ;
this.add(vpii) ;
gridbag.setConstraints(vpii, plugInCnstraints) ;
} catch (FileReadingException fre) {
this.model.displayDebugInformation(fre) ;
}// try
}// for
}// buildGenerators(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 generator to the list of selected generators.
* If the generator is already in the list, nothing is done.
* Activates the delete button if necessary.
*
* @param generator
* generator which will be added to the list
*/
protected void addGenerator(PlugInInfo generator) {
if (!this.selectedGenerators.contains(generator)) {
this.selectedGenerators.add(generator) ;
}// if
this.delete.setEnabled(true) ;
}// addGenerator(PlugInInfo)
/**
* Removes the given generator from the list of selected generators.
* If the generator is not in the list, nothing is done.
* Deactivates the delete button is necessary.
*
* @param generator
* generator which will be removed from the list
*/
protected void removeGenerator(PlugInInfo generator) {
this.selectedGenerators.remove(generator) ;
this.delete.setEnabled(!this.selectedGenerators.isEmpty()) ;
}// removeGenerator(PlugInInfo)
/**
* Returns the model of this view.
*
* @return Model of this view
*/
protected Model getModel() {
return this.model ;
}// getModel()
/**
* Listener for clicks on the check boxes of this frame.
* Updates the list of selected generators accordingly.
*
* @author Schnell Michaël
* @version 1.0
*/
private class CheckBoxListener implements ItemListener {
/**
* Parent frame of the component attached to this listener.
*/
protected DeleteGeneratorFrame 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(DeleteGeneratorFrame parent,
ViewPlugInInfo view, PlugInInfo info) {
super();
this.parent = parent ;
this.view = view ;
this.info = info ;
}// CheckBoxListener(DeleteGeneratorFrame)
/**
* Updates the list of selected generators (add a new generator
* 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.addGenerator(this.info) ;
this.view.setIconIsGrayed(false) ;
} else {
this.parent.removeGenerator(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 generators 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 DeleteGeneratorFrame parent ;
/**
* /**
* Sole constructor.
*
* @param parent
* parent frame of the component attached to this
* listener
*/
public DeleteListener(DeleteGeneratorFrame parent) {
super() ;
this.parent = parent ;
}// DeleteListener(DeleteGeneratorFrame)
/**
* Removes the selected generators (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 generator : this.parent.selectedGenerators) {
this.parent.model.removeGenerator(generator.getId()) ;
}// for
this.parent.dispose() ;
}// actionPerformed(ActionEvent)
}// class DeleteListener
}// class DeleteGeneratorFrame