package de.FeatureModellingTool.ProjectManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import model.FileModel;
/**
* @author baipeng {baipeng8608@gmail.com}
*/
public class JModelChooser extends JDialog{
public int modelID = -1 ;
/**
* Return value if approve (yes, ok) is chosen.
*/
public static final int APPROVE_OPTION = 0;
/**
* Instruction to approve the current selection
* (same as pressing yes or ok).
*/
public static final String APPROVE_SELECTION = "ApproveSelection";
/**
* Return value if an error occured.
*/
public static final int ERROR_OPTION = -1;
private int returnValue = ERROR_OPTION;
public JModelChooser(){
setModal(true);//��ģʽ�Ի���
}
public int getSelectedModelID(){
return modelID;
}
public int showOpenDialog(JComponent parent ,List<FileModel>models){
JPanel pane = createUI(models);
setContentPane(pane);
setVisible(true);
return returnValue;
}
private JPanel createUI(List<FileModel> models){
JPanel box = new JPanel();
box.setSize(100, 100);
box.setLayout(new BoxLayout(box,BoxLayout.PAGE_AXIS));
final ButtonGroup group = new ButtonGroup();
final int numbuts = models.size();
final JRadioButton[]radioButtons = new JRadioButton[numbuts];
for(int i = 0 ;i<numbuts;i++){
FileModel fm = models.get(i);
String text = fm.getId() + " " +fm.getName();
radioButtons[i] = new JRadioButton(text);
group.add(radioButtons[i]);
box.add(radioButtons[i]);
}
radioButtons[0].setSelected(true);
JPanel pane = new JPanel();
pane.setPreferredSize(new Dimension(100,100));
pane.setLayout(new BorderLayout());
pane.add(box,BorderLayout.PAGE_START);
JButton confirm = new JButton("ȷ��");
confirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(int i = 0 ;i<numbuts;i++){
if(radioButtons[i].isSelected()){
String text = radioButtons[i].getText();
int idindex = text.indexOf(' ');
modelID = Integer.valueOf(text.substring(0, idindex)).intValue();
System.out.println("Confirm ModelID="+modelID);
returnValue = APPROVE_OPTION;
dispose();//Close the Dialog.
}
}
}
});
pane.add(confirm,BorderLayout.PAGE_END);
return pane;
}
}