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

📄 spawndialog.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.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.JDFConstants;
import org.cip4.jdflib.core.XMLDoc;
import org.cip4.jdflib.datatypes.VJDFAttributeMap;
import org.cip4.jdflib.node.JDFNode;

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

public class SpawnDialog extends JPanel implements ActionListener
{
	private static final long serialVersionUID = -267165456151780440L;
	
	private JTextField idPath, rootPath; 
    private JButton browse1, browse2; 
    File originalFile;

    File newPartFile;

    File newRootFile;

    File fileToOpen; 
    private JDFNode selectedJDF, originalJDF, spawnedPartJDF;
    private ResourceBundle littleBundle;
    private GridBagLayout layout;
    private GridBagConstraints constraints;
    private JDFFrame parFrame;
        
    public SpawnDialog(final JDFFrame parent, final File origFile, final ResourceBundle bundle, 
    						final JDFNode selectJDF, boolean spawnInformative)
    {
        super();
        this.selectedJDF = selectJDF;
        this.originalFile = origFile;
        this.littleBundle = bundle;
        this.parFrame = parent;
                
        final XMLDoc originalDoc = selectJDF.getOwnerDocument_KElement();
        this.originalJDF = (JDFNode) originalDoc.getRoot();
        
        init();
        
        final String[] options = { littleBundle.getString("OkKey"), littleBundle.getString("CancelKey") };
        
        final int option = JOptionPane.showOptionDialog(parent, this, "Spawn",
            JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);

        if (option == JOptionPane.OK_OPTION)
        {
            String path = idPath.getText();
            if (path==null || path.equals(JDFConstants.EMPTYSTRING))
                path = "SpawnedJDF.jdf";
            
            String rPath = rootPath.getText();
            if (rPath==null || rPath.equals(JDFConstants.EMPTYSTRING))
                rPath = "MainJDF.jdf";
            
            newPartFile = checkedFile(new File(path));
            newRootFile = checkedFile(new File(rPath));
            
            if (newPartFile == null || newRootFile == null) 
            {
                JOptionPane.showMessageDialog(parent, littleBundle.getString("SpawningFailedKey"), "Error", JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                try
                {
                    if (spawnInformative)
                    {
                    	spawnedPartJDF = selectedJDF.spawnInformative(originalFile.toURI().toString(), newPartFile.toURI().toString(),
                                new VJDFAttributeMap(), false, true, true, true);
                    }
                    else 
                    {
	                	spawnedPartJDF = selectedJDF.spawn(originalFile.toURI().toString(), newPartFile.toURI().toString(),
	                        new Vector(), new VJDFAttributeMap(), false, true, true, true);
                    }
                    spawnedPartJDF.eraseEmptyNodes(true);
                    final XMLDoc outDoc_part1 = spawnedPartJDF.getOwnerDocument_KElement();
                    outDoc_part1.write2File(newPartFile.getAbsolutePath(), 0);
                    
                    originalJDF.eraseEmptyNodes(true);
                    originalDoc.write2File(newRootFile.getAbsolutePath(), 0);
                   
                    setOpenFileDialog();
                    
                }
                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);
                }   
            }
        }
    }
   
    
    /**
     * Creates the fields and view for the Spawn Dialog and also the default
     * file names for the jdfFile and partFile.
     */
    private void init()
    {
    	layout = new GridBagLayout(); 
        setLayout(layout);
        constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(3,5,3,5);
        setBorder(BorderFactory.createTitledBorder(littleBundle.getString("SpawnedOutputKey")));
        
        final JLabel mergeLabel = new JLabel(createPathName(littleBundle.getString("SpawnedOutputKey").length()));
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        layout.setConstraints(mergeLabel, constraints);
        add(mergeLabel);
       
        final JLabel idLabel = new JLabel(littleBundle.getString("SpawnedJDFKey"));
        constraints.insets = new Insets(10,5,3,5);
        layout.setConstraints(idLabel, constraints);
        add(idLabel);
  
        final Box idBox = Box.createHorizontalBox();
        
        newPartFile = new File(createFileName(".ID_" + selectedJDF.getID()));
        int col = newPartFile.getName().length() < 35 ? newPartFile.getName().length() : 35;
        idPath = new JTextField(newPartFile.getName(), col);
        
        idBox.add(idPath);
        idBox.add(Box.createHorizontalStrut(10));
        
        browse1 = new JButton(littleBundle.getString("BrowseKey"));
        browse1.setPreferredSize(new Dimension(85,22));
        browse1.addActionListener(this);
        idBox.add(browse1);
        
        constraints.insets = new Insets(0,5,8,5);
        layout.setConstraints(idBox, constraints);
        add(idBox);
        
        
        final JLabel rLabel = new JLabel(littleBundle.getString("MainJDFKey"));
        constraints.insets = new Insets(10,5,3,5);
        layout.setConstraints(rLabel, constraints);
        add(rLabel);
  
        final Box idBox2 = Box.createHorizontalBox();
        
        newRootFile = new File(createFileName(".ID_" + originalJDF.getID()));
        int col2 = newRootFile.getName().length() < 35 ? newRootFile.getName().length() : 35;
        rootPath = new JTextField(newRootFile.getName(), col2);
        
        idBox2.add(rootPath);
        idBox2.add(Box.createHorizontalStrut(10));
        
        browse2 = new JButton(littleBundle.getString("BrowseKey"));
        browse2.setPreferredSize(new Dimension(85,22));
        browse2.addActionListener(this);
        idBox2.add(browse2);
        
        constraints.insets = new Insets(0,5,8,5);
        layout.setConstraints(idBox2, constraints);
        add(idBox2);
        
        setVisible(true);
 
    }
    
    
    private void setOpenFileDialog()
    {
        JPanel panel = new JPanel();
    	GridBagLayout dialogLayout = new GridBagLayout(); 
//        GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(3,5,3,5);
    	dialogLayout.setConstraints(panel, constraints);
    	panel.setLayout(dialogLayout);
    	
    	panel.setBorder(BorderFactory.createTitledBorder(littleBundle.getString("ChooseSpawnedFileKey")));
              
    	final Box verticalBox = Box.createVerticalBox();
   
    	verticalBox.add(Box.createVerticalStrut(10));
	    ButtonGroup group = new ButtonGroup();
	       
	    JRadioButton original, mainSpawned, partSpawned;
	    group.add(original = new JRadioButton(littleBundle.getString("ShowOriginalFileKey")));
	    original.setSelected(true);
	    original.addItemListener(new ItemListener()
	    		{
	      			public void itemStateChanged(ItemEvent e)
	      		    {
	      		    	if (e.getStateChange() == ItemEvent.SELECTED)
	      		    	{
	      		    		fileToOpen = originalFile;
	      		    	}
	      		    }
	      		});
	    verticalBox.add(original);
	          
	    group.add(mainSpawned = new JRadioButton(littleBundle.getString("ShowMainFileKey")));
	    mainSpawned.addItemListener(new ItemListener()
      		{
      			public void itemStateChanged(ItemEvent e)
      		    {
      		    	if (e.getStateChange() == ItemEvent.SELECTED)
      		    	{
      		    		fileToOpen = newRootFile; 
      		    	}
      		    }
      		});
	    verticalBox.add(mainSpawned);
	    
	    group.add(partSpawned = new JRadioButton(littleBundle.getString("ShowSpawnedFileKey")));
	    partSpawned.addItemListener(new ItemListener()
      		{
      			public void itemStateChanged(ItemEvent e)
      		    {
      		    	if (e.getStateChange() == ItemEvent.SELECTED)
      		    	{
      		    		fileToOpen = newPartFile; 
      		    	}
      		    }
      		});
	    verticalBox.add(partSpawned);
	    panel.add(verticalBox);
     
	    final String[] options = { littleBundle.getString("OkKey"), littleBundle.getString("CancelKey") };
        
        final int option = JOptionPane.showOptionDialog(parFrame, panel, littleBundle.getString("SpawningCompletedKey"),
            JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);

        if (option != JOptionPane.OK_OPTION)
        {
        	fileToOpen = originalFile;
        }
        
    }
    
    public File getFileToOpen()
    {
    	return fileToOpen;
    }
    
    /**
     * Create the default file name including its absolute path. The String addOn
     * is added just ahead of the file's extension.
     * @param addOn - The String to add to the original file name.
     * @return The file name with the addon.
     */
    private String createFileName(String addOn)
    {
        int index = originalFile.getAbsolutePath().lastIndexOf('\\');
        String path = originalFile.getAbsolutePath().substring(0, index + 1);
        
        index = originalFile.getName().lastIndexOf('.');
        String name = originalFile.getName().substring(0, index);
        String extension = originalFile.getName().substring(index, originalFile.getName().length());
        
        return path + name + addOn + extension;
    }
    /**
     * 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)
    {
        String s = '"' + originalFile.getAbsolutePath() + '"';
        
        if (s.length() <= 1.5 * length)
            return s;

        int i = s.indexOf('\\');
        int j = s.lastIndexOf('\\');
        
        if (i == j)
            return s.substring(0, length - 4) + "..." + '"';
        
        String start = s.substring(0, i + 1);
        String end = s.substring(j, s.length());
        
        return start + "..." + end;
    }
    
    
    private File checkedFile(File f)
    {
        File result = null;
        
        if (f != null && !f.isDirectory())
        {
            String fileName = f.getAbsolutePath().toLowerCase();
            
            if (fileName.endsWith(".jdf")||fileName.endsWith(".xml"))
            {
                result = f;
            }
            else if (fileName.indexOf(".")==-1)
            {
               result = new File(f.getAbsolutePath().concat(".jdf"));
            }
        }
       
        return result;
    }
    
      
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == browse1 || e.getSource() == browse2)
        {
            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);
            
            if (e.getSource() == browse1)
            {
                files.setSelectedFile(newPartFile);
            }
            else if (e.getSource() == browse2)
            {
                files.setSelectedFile(newRootFile);
            }
            
            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);
            
            final int option = files.showOpenDialog(parFrame);
            
            if (option == JFileChooser.APPROVE_OPTION)
            {
                if (e.getSource() == browse1)
                {
                    newPartFile = checkedFile(files.getSelectedFile());
                    idPath.setText(files.getSelectedFile().getAbsolutePath());
                }
                else if (e.getSource() == browse2)
                {
                    newRootFile = checkedFile(files.getSelectedFile());
                    rootPath.setText(files.getSelectedFile().getAbsolutePath());
                }
                
            }
            else if (option == JFileChooser.ERROR_OPTION) 
            {
                JOptionPane.showMessageDialog(parFrame, "Spawn failed", 
                        "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

⌨️ 快捷键说明

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