⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 devicecapdialog.java

📁 java xml 应用开发
💻 JAVA
字号:
import java.awt.Dimension;
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.io.File;
import java.util.ResourceBundle;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.cip4.jdflib.core.ElementName;
import org.cip4.jdflib.core.JDFDoc;
import org.cip4.jdflib.core.JDFParser;
import org.cip4.jdflib.core.KElement;
import org.cip4.jdflib.core.VElement;
import org.cip4.jdflib.core.XMLDoc;
import org.cip4.jdflib.core.KElement.EnumValidationLevel;
import org.cip4.jdflib.datatypes.JDFBaseDataTypes.EnumFitsValue;
import org.cip4.jdflib.jmf.JDFJMF;
import org.cip4.jdflib.node.JDFNode;
import org.cip4.jdflib.resource.devicecapability.JDFDeviceCap;
import org.cip4.jdflib.util.XMLstrm;

/**
 * DeviceCapsDialog.java
 * @author Elena Skobchenko
 */

public class DeviceCapDialog extends JPanel implements ActionListener
{
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = -267165456151780040L;
    
    private JTextField idPath;
    private JButton browse; 
    private File file;
    private File idFile;
    private ResourceBundle littleBundle;
    private GridBagLayout outLayout = new GridBagLayout(); 
    private GridBagConstraints outConstraints = new GridBagConstraints();
    private VElement executableJDF = null;
    private XMLDoc bugReport = null;
    private JDFFrame parFrame;
    
    private JComboBox chooseValidLevel;
    EnumFitsValue testlists = EnumFitsValue.Allowed;
    private EnumValidationLevel validationLevel = KElement.EnumValidationLevel.RecursiveComplete;
    
    public DeviceCapDialog(final JDFFrame parent, final File _file, final ResourceBundle bundle, final JDFNode docRoot)
    {
        super();
        this.file = _file;
        this.littleBundle = bundle;
        this.parFrame = parent;
        
        init();
        
        final String[] options = { littleBundle.getString("OkKey"), littleBundle.getString("CancelKey") };
        
        final int option = JOptionPane.showOptionDialog(parent, this, "Test against DeviceCapabilities file",
            JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
            
        if (option == JOptionPane.OK_OPTION)
        {
            idFile = checkedFile(new File(idPath.getText()));

            if (idFile == null) 
            {
                // no action
            }
            else if (idFile.exists())
            {
                final JDFParser parser = new JDFParser();
                
                try 
                {
                    final JDFDoc devCapDoc = parser.parseFile(idFile.getAbsolutePath());
                    
                    JDFJMF jmfRoot = null;
                    
                    if (devCapDoc!=null) 
                    {
                        jmfRoot = devCapDoc.getJMFRoot();

                        if (jmfRoot==null) 
                        {
                            JOptionPane.showMessageDialog(parent, "Chosen file is not a JMF file", 
                                    "Error", JOptionPane.ERROR_MESSAGE);
                        }
                        else if (jmfRoot.getDeepElement(ElementName.DEVICECAP, null, 0)==null)
                        {
                            JOptionPane.showMessageDialog(parent, 
                                    "File does not contain Device Capability description", 
                                    "Error", JOptionPane.ERROR_MESSAGE);
                        }
                        else 
                        {
                            final JDFDeviceCap deviceCap = 
                                (JDFDeviceCap) jmfRoot.getDeepElement(ElementName.DEVICECAP, null, 0);
                	        	
                            executableJDF = deviceCap.getExecutableJDF(docRoot,testlists,validationLevel);
                            bugReport = deviceCap.getBadJDFInfo(docRoot,testlists,validationLevel);
                        }         
                    }                  
                }
                catch (Exception e) 
                {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(parent, 
                            "An internal error occured: \n" + e.getClass() + " \n"
                             + (e.getMessage()!=null ? ("\"" + e.getMessage() + "\"") : ""), 
                            "Error", JOptionPane.ERROR_MESSAGE);
                }                 
                
                XMLstrm.xmlIndent(2, true);
            }
            else 
            {
                JOptionPane.showMessageDialog(parent, "File is not accepted", 
                        "Error", JOptionPane.ERROR_MESSAGE);
            }
            
        }
        
    }
    
    /**
     * Creates the fields and view for the Merge Dialog.
     */
    private void init()
    {

        final JPanel panel = new JPanel();

        outConstraints.fill = GridBagConstraints.BOTH;
        outConstraints.insets = new Insets(0,0,10,0);

        outLayout.setConstraints(panel, outConstraints);
        setLayout(outLayout);

        final GridBagLayout inLayout = new GridBagLayout();    	   	

        panel.setLayout(inLayout);
        
        final GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(3,5,3,5);
        panel.setBorder(BorderFactory.createTitledBorder(littleBundle.getString("DCInputKey")));
        
        final JLabel dLabel = new JLabel(createPathName(littleBundle.getString("DCInputKey").length()));
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        inLayout.setConstraints(dLabel, constraints);
        panel.add(dLabel);
       
        final JLabel idLabel = new JLabel(littleBundle.getString("DCFileKey"));
        constraints.insets = new Insets(10,5,3,5);
        inLayout.setConstraints(idLabel, constraints);
        panel.add(idLabel);
  
        final Box horizontalBox = Box.createHorizontalBox();
        idPath = new JTextField(35);
        horizontalBox.add(idPath);
        horizontalBox.add(Box.createHorizontalStrut(10));
        
        browse = new JButton(littleBundle.getString("BrowseKey"));
        browse.setPreferredSize(new Dimension(85,22));
        browse.addActionListener(this);
        horizontalBox.add(browse);
        
        constraints.insets = new Insets(0,5,8,5);
        inLayout.setConstraints(horizontalBox, constraints);
        panel.add(horizontalBox);
        
        add(panel);
        
        final JPanel downPanel = new JPanel();    	    	
        outConstraints.gridwidth = GridBagConstraints.REMAINDER;
        downPanel.setLayout(outLayout);
        
        JPanel testLists = new JPanel();
        testLists.setLayout(inLayout);
        
        testLists.setBorder(BorderFactory.createTitledBorder(littleBundle.getString("DCTestListsKey")));
        
//        final Box verticalBox = Box.createVerticalBox();
        final ButtonGroup group = new ButtonGroup();
        
        JRadioButton allowedButton;
        JRadioButton presentButton;
        group.add(allowedButton = new JRadioButton("Allowed"));
        allowedButton.setSelected(true);
        allowedButton.addItemListener(new ItemListener()
                {
                    public void itemStateChanged(ItemEvent e)
                    {
                        if (e.getStateChange() == ItemEvent.SELECTED)
                            testlists = EnumFitsValue.Allowed; 
                    }
                });
//        verticalBox.add(allowedButton);
        testLists.add(allowedButton);
        
        group.add(presentButton = new JRadioButton("Present"));
        presentButton.addItemListener(new ItemListener()
            {
                public void itemStateChanged(ItemEvent e)
                {
                    if (e.getStateChange() == ItemEvent.SELECTED)
                        testlists = EnumFitsValue.Present; 
                }
            });
//        verticalBox.add(presentButton);
        testLists.add(presentButton);
       
//        testLists.add(verticalBox);
        outLayout.setConstraints(testLists, outConstraints);
        downPanel.add(testLists);
      
        final JPanel validationPanel = new JPanel();
        validationPanel.setBorder(BorderFactory.createTitledBorder(littleBundle.getString("ValidationLevelKey")));
                
        final Vector allowedValues = EnumValidationLevel.getNamesVector();
        allowedValues.removeElementAt(0);
        chooseValidLevel = new JComboBox(allowedValues);
        chooseValidLevel.setSelectedItem(KElement.EnumValidationLevel.RecursiveComplete.getName());
        chooseValidLevel.addActionListener(this);
        validationPanel.add(chooseValidLevel);
        outLayout.setConstraints(validationPanel, outConstraints);
        
        downPanel.add(validationPanel);
        
        add(downPanel);
        setVisible(true);
         
    }
    
    /**
     * Creates the String which is to be displayed...
     * @param length - The length of the title...
     * @return The file name, may be a little bit altered.
     */
    private String createPathName(int length)
    {
        final String s = '"' + file.getAbsolutePath() + '"';
        
        if (s.length() <= 1.5 * length)
            return s;

        final int i = s.indexOf('\\');
        final int j = s.lastIndexOf('\\');
        
        if (i == j)
            return s.substring(0, length - 4) + "..." + '"';
        
        final String start = s.substring(0, i + 1);
        final String end = s.substring(j, s.length());
        
        return start + "..." + end;
    }
    
    private File checkedFile(File f)
    {
        File result = null;
        
        if (f != null && !f.isDirectory())
        {
            final String fileName = f.getAbsolutePath().toLowerCase();
            
            if (fileName.endsWith(".jdf")||fileName.endsWith(".xml"))
            {
                result = f;
            }
        }
       
        return result;
    }
    
    public void actionPerformed(ActionEvent e)
    {
        final Object source = e.getSource();
        if (source == browse)
        {
            final JFileChooser files = new JFileChooser();
            
            files.setApproveButtonText(littleBundle.getString("OkKey"));
            files.setApproveButtonMnemonic('O');
            files.setDialogTitle(littleBundle.getString("BrowseKey"));
            files.setFileSelectionMode(JFileChooser.FILES_ONLY);
            files.rescanCurrentDirectory();
            files.setMultiSelectionEnabled(false);
            
            final JDFFileFilter xmlFilter = new JDFFileFilter();
            xmlFilter.addExtension("xml");
            xmlFilter.setDescription("XML files");
            files.addChoosableFileFilter(xmlFilter);

            final JDFFileFilter jdfFilter = new JDFFileFilter();
            jdfFilter.addExtension("jdf");
            jdfFilter.setDescription("JDF files");
            files.addChoosableFileFilter(jdfFilter);
            
            files.setCurrentDirectory(file);

            final int option = files.showOpenDialog(parFrame);
            
            if (option == JFileChooser.APPROVE_OPTION)
            {
//                file = files.getCurrentDirectory();
                idPath.setText(files.getSelectedFile().getAbsolutePath());
            }
            else if (option == JFileChooser.ERROR_OPTION) 
            {
                JOptionPane.showMessageDialog(parFrame, "File is not accepted", 
                        "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
        else if (source == chooseValidLevel)
        {
            validationLevel = EnumValidationLevel.getEnum((String)chooseValidLevel.getSelectedItem());
        }
    }
    
    public VElement getExecutable() 
    {
        return executableJDF;
    }
    
    public XMLDoc getBugReport() 
    {
        return bugReport;
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -