/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* DrawingChooserDialog.java
*
* Created on 28 mai 2011, 08:45:56
*/
package feuille.karaoke.dialog;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import feuille.drawing.adf.DrawingObject;
import feuille.drawing.adf.LayerContent;
import feuille.drawing.adf.XmlDrawingHandler;
import feuille.lib.Language;
import feuille.karaoke.lib.MiniDraw;
import feuille.karaoke.lib.PresenceDrawPanel;
import feuille.karaoke.renderer.drawingChooserRenderer;
/**
* <p>This is a dialog for the choice of drawing.<br />
* C'est une boîte de dialogue pour le choix de dessin.</p>
* @author The Wingate 2940
*/
public class DrawingChooserDialog extends javax.swing.JDialog {
private ButtonPressed bp;
private Language localeLanguage = feuille.MainFrame.getLanguage();
private DefaultListModel dlm;
public enum ButtonPressed{
NONE, OK_BUTTON, CANCEL_BUTTON;
}
/** <p>Creates new form DrawingChooserDialog.<br />
* Crée un nouveau formaulaire DrawingChooserDialog.</p> */
public DrawingChooserDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
dlm = new DefaultListModel();
jList1.setModel(dlm);
jList1.setCellRenderer(new drawingChooserRenderer());
bp = ButtonPressed.NONE;
//Force visibility
setAlwaysOnTop(true);
//Set language
if(localeLanguage.getValueOf("titleDCD")!=null){setTitle(localeLanguage.getValueOf("titleDCD"));}
if(localeLanguage.getValueOf("buttonOk")!=null){OK_Button.setText(localeLanguage.getValueOf("buttonOk"));}
if(localeLanguage.getValueOf("buttonCancel")!=null){Cancel_Button.setText(localeLanguage.getValueOf("buttonCancel"));}
if(localeLanguage.getValueOf("checkboxP1P0")!=null){chkInclude.setText(localeLanguage.getValueOf("checkboxP1P0"));}
if(localeLanguage.getValueOf("buttonDirectory")!=null){btnDirectory.setText(localeLanguage.getValueOf("buttonDirectory"));}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
fcDirectory = new javax.swing.JFileChooser();
OK_Button = new javax.swing.JButton();
Cancel_Button = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
btnDirectory = new javax.swing.JButton();
chkInclude = new javax.swing.JCheckBox();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Choose a drawing");
OK_Button.setText("OK");
OK_Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OK_ButtonActionPerformed(evt);
}
});
Cancel_Button.setText("Cancel");
Cancel_Button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Cancel_ButtonActionPerformed(evt);
}
});
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(jList1);
btnDirectory.setText("Change the directory...");
btnDirectory.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDirectoryActionPerformed(evt);
}
});
chkInclude.setSelected(true);
chkInclude.setText("Include {\\p1} and {\\p0}");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(chkInclude, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(48, 48, 48)
.addComponent(OK_Button, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Cancel_Button, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnDirectory, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField1)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnDirectory)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(chkInclude)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Cancel_Button)
.addComponent(OK_Button))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void OK_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OK_ButtonActionPerformed
bp = ButtonPressed.OK_BUTTON;
dispose();
}//GEN-LAST:event_OK_ButtonActionPerformed
private void Cancel_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Cancel_ButtonActionPerformed
bp = ButtonPressed.CANCEL_BUTTON;
dispose();
}//GEN-LAST:event_Cancel_ButtonActionPerformed
private void btnDirectoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDirectoryActionPerformed
fcDirectory.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fcDirectory.setAccessory(new PresenceDrawPanel(fcDirectory));
// Action
int z = fcDirectory.showOpenDialog(this);
if (z == JFileChooser.APPROVE_OPTION){
refreshList(fcDirectory.getSelectedFile().getAbsolutePath());
}
}//GEN-LAST:event_btnDirectoryActionPerformed
/** <p>Show the dialog and try to return ASS commands of the drawing mode.<br />
* Montre la boîte de dialogue et essaie de retourner les commandes ASS du mode dessin.</p> */
public String showDialog(){
setVisible(true);
if(bp.equals(ButtonPressed.OK_BUTTON)){
if(jTextField1.getText().isEmpty()==false){
if(chkInclude.isSelected()==true){
return "{\\p1}"+jTextField1.getText()+"{\\p0}";
}else{
return jTextField1.getText();
}
}else if(jList1.getSelectedIndex()!=-1){
MiniDraw md = (MiniDraw)jList1.getSelectedValue();
if(chkInclude.isSelected()==true){
return "{\\p1}"+md.getCommands()+"{\\p0}";
}else{
return md.getCommands();
}
}
return "";
}else{
return null;
}
}
public String showDialog(String commands){
jTextField1.setText(commands);
setVisible(true);
if(bp.equals(ButtonPressed.OK_BUTTON)){
if(jTextField1.getText().isEmpty()==false){
if(chkInclude.isSelected()==true){
return "{\\p1}"+jTextField1.getText()+"{\\p0}";
}else{
return jTextField1.getText();
}
}else if(jList1.getSelectedIndex()!=-1){
MiniDraw md = (MiniDraw)jList1.getSelectedValue();
if(chkInclude.isSelected()==true){
return "{\\p1}"+md.getCommands()+"{\\p0}";
}else{
return md.getCommands();
}
}
return commands;
}else{
return commands;
}
}
/** <p>Set a path.<br />Définit un chemin.</p> */
public void setPath(String path){
refreshList(path);
}
// /** <p>Open a drawing file.<br />Ouvre un fichier dessin.</p> */
// private java.util.List<String> openDrawingFile(String path){
// String line = "";
// java.util.List<String> list = new java.util.ArrayList<String>();
// File file = new File(path);
// try{
// FileInputStream fis = new FileInputStream(file);
// java.io.BufferedReader br = new java.io.BufferedReader(
// new java.io.InputStreamReader(fis, "UTF-8"));
// while((line = br.readLine())!=null){
// String[] fragments = line.split(";");
// list.add(fragments[2]);
// }
// br.close(); fis.close();
// return list;
// }catch (Exception exc){
// return list;
// }
// }
public String[] getAssCommandsOfFile(String path){
try{
XmlDrawingHandler xdh = new XmlDrawingHandler(path);
DrawingObject dro = xdh.getDrawingObject();
String[] table = new String[dro.getLayers().size()];
int count = 0;
for(LayerContent lc : dro.getLayers()){
table[count] = lc.getAssCommands();
count += 1;
}
return table;
}catch(Exception ex){
return null;
}
}
/** <p>Refresh the list.<br />Refraichit la liste.</p> */
private void refreshList(String path){
dlm.clear();
File dir = new File(path);
for(File f : dir.listFiles()){
if(f.getName().endsWith(".adf")){
//java.util.List<String> list = openDrawingFile(f.getAbsolutePath());
String[] list = getAssCommandsOfFile(f.getAbsolutePath());
if(list!=null){
for(String commands : list){
MiniDraw md = new MiniDraw(f, commands);
dlm.addElement(md);
}
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
DrawingChooserDialog dialog = new DrawingChooserDialog(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton Cancel_Button;
private javax.swing.JButton OK_Button;
private javax.swing.JButton btnDirectory;
private javax.swing.JCheckBox chkInclude;
private javax.swing.JFileChooser fcDirectory;
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}