/* Copyright 2004-2014 Jim Voris * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.qumasoft.guitools.qwin.dialog; import com.qumasoft.guitools.qwin.FileGroupDataElement; import com.qumasoft.guitools.qwin.FileGroupManager; import com.qumasoft.guitools.qwin.QWinFrame; import com.qumasoft.guitools.qwin.QWinUtility; import com.qumasoft.qvcslib.QVCSException; import com.qumasoft.qvcslib.Utility; import java.awt.Component; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.logging.Level; import javax.swing.JLabel; import javax.swing.JTable; import javax.swing.UIManager; /** * Define file groups dialog. * @author Jim Voris */ public class DefineFileGroupsDialog extends AbstractQWinCommandDialog { private static final long serialVersionUID = 889436229138196210L; private final Map<String, FileGroupDataElement> fileGroups = Collections.synchronizedMap(new TreeMap<String, FileGroupDataElement>()); private final Set<String> extensionsSet = Collections.synchronizedSet(new HashSet<String>()); /** * Create a new define file groups dialog. * @param parent the parent frame. * @param modal is this modal. */ public DefineFileGroupsDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); initFileGroups(); initComponents(); populateComponents(); setFont(); center(); } /** * 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. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { enableFileGroupsCheckBox = new javax.swing.JCheckBox(); definedFileGroupsLabel = new javax.swing.JLabel(); fileGroupsScrollPane = new javax.swing.JScrollPane(); fileGroupsTable = new javax.swing.JTable(); okButton = new javax.swing.JButton(); cancelButton = new javax.swing.JButton(); newButton = new javax.swing.JButton(); editButton = new javax.swing.JButton(); deleteButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Define File Groups"); setResizable(false); enableFileGroupsCheckBox.setText("Enable File Groups"); enableFileGroupsCheckBox.setToolTipText("Enable the check box to turn on recognition of defined file groups"); enableFileGroupsCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0)); definedFileGroupsLabel.setText("Defined File Groups:"); fileGroupsTable.setModel(new FileGroupDialogTableModel(fileGroups)); fileGroupsScrollPane.setViewportView(fileGroupsTable); okButton.setText("OK"); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okButtonActionPerformed(evt); } }); cancelButton.setText("Cancel"); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelButtonActionPerformed(evt); } }); newButton.setText("New..."); newButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { newButtonActionPerformed(evt); } }); editButton.setText("Edit..."); editButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { editButtonActionPerformed(evt); } }); deleteButton.setText("Delete"); deleteButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { deleteButtonActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(fileGroupsScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(22, 22, 22) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false) .add(okButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE) .add(cancelButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)) .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() .add(2, 2, 2) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, newButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE) .add(org.jdesktop.layout.GroupLayout.TRAILING, editButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE) .add(deleteButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))))) .add(enableFileGroupsCheckBox) .add(definedFileGroupsLabel)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(enableFileGroupsCheckBox) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(definedFileGroupsLabel) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) .add(layout.createSequentialGroup() .add(okButton) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(cancelButton) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(newButton) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(editButton) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(deleteButton)) .add(fileGroupsScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 149, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_deleteButtonActionPerformed {//GEN-HEADEREND:event_deleteButtonActionPerformed int[] selectedRows = fileGroupsTable.getSelectedRows(); if (selectedRows.length == 1) { FileGroupDialogTableModel dataModel = (FileGroupDialogTableModel) fileGroupsTable.getModel(); int index = selectedRows[0]; String groupName = dataModel.getGroupName(index); deleteGroup(groupName); } }//GEN-LAST:event_deleteButtonActionPerformed private void editButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editButtonActionPerformed {//GEN-HEADEREND:event_editButtonActionPerformed int[] selectedRows = fileGroupsTable.getSelectedRows(); if (selectedRows.length == 1) { FileGroupDialogTableModel dataModel = (FileGroupDialogTableModel) fileGroupsTable.getModel(); int index = selectedRows[0]; String groupName = dataModel.getGroupName(index); String[] extensions = dataModel.getExtensions(index); EditFileGroupDialog editFileGroupDialog = new EditFileGroupDialog(QWinFrame.getQWinFrame(), this, groupName, extensions); editFileGroupDialog.setVisible(true); } }//GEN-LAST:event_editButtonActionPerformed private void newButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_newButtonActionPerformed {//GEN-HEADEREND:event_newButtonActionPerformed EditFileGroupDialog editFileGroupDialog = new EditFileGroupDialog(QWinFrame.getQWinFrame(), this); editFileGroupDialog.setVisible(true); fileGroupsTable.clearSelection(); }//GEN-LAST:event_newButtonActionPerformed private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed {//GEN-HEADEREND:event_cancelButtonActionPerformed dismissDialog(); }//GEN-LAST:event_cancelButtonActionPerformed private void okButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_okButtonActionPerformed {//GEN-HEADEREND:event_okButtonActionPerformed FileGroupManager.getInstance().removeAllGroups(); Iterator<FileGroupDataElement> it = fileGroups.values().iterator(); while (it.hasNext()) { FileGroupDataElement element = it.next(); FileGroupManager.getInstance().storeGroup(element.getGroupName(), element.getExtensions()); } // Save the state of the enabled flag. boolean enableFileGroupsFlag = enableFileGroupsCheckBox.isSelected(); FileGroupManager.getInstance().setEnabledFlag(enableFileGroupsFlag); dismissDialog(); }//GEN-LAST:event_okButtonActionPerformed @Override public void dismissDialog() { setVisible(false); dispose(); } private void initFileGroups() { int existingGroupCount = FileGroupManager.getInstance().getGroupCount(); for (int i = 0; i < existingGroupCount; i++) { String groupName = FileGroupManager.getInstance().getGroupName(i); String[] extensions = FileGroupManager.getInstance().getGroupFileExtensions(i); FileGroupDataElement element = new FileGroupDataElement(groupName, extensions); try { addExtensionsToExtensionSet(extensions); fileGroups.put(groupName, element); } catch (QVCSException e) { QWinUtility.logProblem(Level.WARNING, Utility.expandStackTraceToString(e)); } } } private void populateComponents() { boolean enabledFlag = FileGroupManager.getInstance().getEnabledFlag(); enableFileGroupsCheckBox.setSelected(enabledFlag); fileGroupsTable.setDefaultRenderer(javax.swing.JLabel.class, new CellRenderer()); } private void addExtensionsToExtensionSet(String[] extensions) throws QVCSException { for (String extension : extensions) { if (extensionsSet.contains(extension)) { throw new QVCSException("Extension " + extension + " is already in use on another file group."); } extensionsSet.add(extension); } } private void removeExtensionsFromExtensionSet(String[] extensions) { for (String extension : extensions) { extensionsSet.remove(extension); } } void addGroup(String groupName, String[] extensions) { FileGroupDataElement element = new FileGroupDataElement(groupName, extensions); fileGroups.put(groupName, element); FileGroupDialogTableModel model = (FileGroupDialogTableModel) fileGroupsTable.getModel(); model.fireTableDataChanged(); } void deleteGroup(String groupName) { FileGroupDataElement element = fileGroups.get(groupName); assert (element != null); removeExtensionsFromExtensionSet(element.getExtensions()); fileGroups.remove(groupName); FileGroupDialogTableModel model = (FileGroupDialogTableModel) fileGroupsTable.getModel(); model.fireTableDataChanged(); } void updateGroup(String groupName, String[] extensions) throws QVCSException { FileGroupDataElement oldElement = fileGroups.get(groupName); if (oldElement != null) { removeExtensionsFromExtensionSet(oldElement.getExtensions()); } FileGroupDataElement element = new FileGroupDataElement(groupName, extensions); addExtensionsToExtensionSet(extensions); fileGroups.put(groupName, element); FileGroupDialogTableModel model = (FileGroupDialogTableModel) fileGroupsTable.getModel(); model.fireTableDataChanged(); } class CellRenderer extends javax.swing.table.DefaultTableCellRenderer { private static final long serialVersionUID = 1L; @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { JLabel inputLabel = (JLabel) fileGroupsTable.getModel().getValueAt(row, column); // Set the column alignment. switch (column) { default: { setHorizontalAlignment(javax.swing.SwingConstants.LEFT); break; } } if (isSelected) { super.setForeground(table.getSelectionForeground()); super.setBackground(table.getSelectionBackground()); } else { super.setForeground(table.getForeground()); super.setBackground(table.getBackground()); } setFont(table.getFont()); if (hasFocus) { setBorder(UIManager.getBorder("Table.focusCellHighlightBorder")); if (table.isCellEditable(row, column)) { super.setForeground(UIManager.getColor("Table.focusCellForeground")); super.setBackground(UIManager.getColor("Table.focusCellBackground")); } } else { setBorder(noFocusBorder); } setText(inputLabel.getText()); setIcon(null); return this; } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton cancelButton; private javax.swing.JLabel definedFileGroupsLabel; private javax.swing.JButton deleteButton; private javax.swing.JButton editButton; private javax.swing.JCheckBox enableFileGroupsCheckBox; private javax.swing.JScrollPane fileGroupsScrollPane; private javax.swing.JTable fileGroupsTable; private javax.swing.JButton newButton; private javax.swing.JButton okButton; // End of variables declaration//GEN-END:variables }