import javax.swing.border.*; /** * switchLineSeparatorForAllBuffers.bsh * * Displays a dialog whcih allows you to switch the line separators of either: * * - All open buffers in current view * - All files (recursive) in a specified directory with a filter * - All files in a ProjectViewer project which you select * * Note: The macro will only close files which are not already open. * * @author: Saimon Moore (saimonmoore@yahoo.co.uk) * @since 20/02/2004 * @version: 0.2 */ void showDialog() { //For anonymous classes fview = view; // create dialog object title = "Switch the line separator!!!"; dialog = new JDialog(view, title, false); content = new JPanel(new BorderLayout()); content.setBorder(new EmptyBorder(12, 12, 12, 12)); dialog.setContentPane(content); //Radios Panel radiosPanel = new JPanel(); radiosPanel.setLayout(new BoxLayout(radiosPanel, BoxLayout.PAGE_AXIS)); radiosPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); //Open Buffers Panel openBuffersPanel = new JPanel(); openBuffersPanel.setLayout(new BoxLayout(openBuffersPanel, BoxLayout.LINE_AXIS)); openBuffersPanel.setBorder(BorderFactory.createEmptyBorder(0, 1, 1, 1)); radioOpenFiles = new JRadioButton("Only Open Buffers..."); radioOpenFiles.setSelected(true); radioOpenFiles.addActionListener(this); //openBuffersPanel.add(Box.createHorizontalGlue()); openBuffersPanel.add(radioOpenFiles); openBuffersPanel.add(Box.createHorizontalGlue()); //Directory Panel dirPanel = new JPanel(); dirPanel.setLayout(new BoxLayout(dirPanel, BoxLayout.LINE_AXIS)); dirPanel.setBorder(BorderFactory.createEmptyBorder(0, 1, 1, 1)); textDirectory = new HistoryTextField("macro.sls.directory"); d = new Dimension(100, 20); textDirectory.setPreferredSize(d); chooserButton = new JButton("..."); chooserButton.addActionListener(this); radioDirFiles = new JRadioButton("All files in directory..."); radioDirFiles.addActionListener(this); //Filter panel filterPanel = new JPanel(); filterPanel.setLayout(new BoxLayout(filterPanel, BoxLayout.PAGE_AXIS)); filterPanel.setBorder(BorderFactory.createEmptyBorder(0, 1, 1, 1)); filterTopPanel = new JPanel(); filterTopPanel.setLayout(new BoxLayout(filterTopPanel, BoxLayout.LINE_AXIS)); filterTopPanel.setBorder(BorderFactory.createEmptyBorder(0, 1, 1, 1)); filterBottomPanel = new JPanel(); filterBottomPanel.setLayout(new BoxLayout(filterBottomPanel, BoxLayout.LINE_AXIS)); filterBottomPanel.setBorder(BorderFactory.createEmptyBorder(0, 1, 1, 1)); filterCheckbox = new JCheckBox("Filter?"); filterCheckbox.setSelected(true) ; filterCheckbox.addActionListener(this); filterTextField = new HistoryTextField("macro.sls.filter"); d = new Dimension(50, 20); filterTextField.setPreferredSize(d); filterTextField.setMaximumSize(d); filterTextField.setText("*.*"); filterTopPanel.add(Box.createHorizontalGlue()); filterTopPanel.add(filterCheckbox); filterTopPanel.add(filterTextField); filterBottomPanel.add(Box.createHorizontalGlue()); filterBottomPanel.add(textDirectory); filterBottomPanel.add(chooserButton); filterPanel.add(filterTopPanel); dirPanel.add(Box.createRigidArea(new Dimension(1, 0))); filterPanel.add(filterBottomPanel); dirPanel.add(radioDirFiles); dirPanel.add(Box.createHorizontalGlue()); dirPanel.add(filterPanel); //Project Panel radioProjectFiles = new JRadioButton("All files in selected project..."); radioProjectFiles.addActionListener(this); //Combobox projectList = new JComboBox(); model = projectList.getModel(); int maxProject = 0; for (Iterator it = projectviewer.ProjectManager.getInstance().getProjects(); it.hasNext(); ) { project = it.next(); projectName = project.getName(); maxProject = projectName.length() > maxProject ? projectName.length() : maxProject; ((DefaultComboBoxModel) model).addElement(project.getName()); } projectList.setSelectedIndex(0); dp = new Dimension(maxProject *5, 20); projectList.setPreferredSize(dp); projectList.setMaximumSize(dp); projectList.addActionListener(this); projectPanel = new JPanel(); projectPanel.setLayout(new BoxLayout(projectPanel, BoxLayout.LINE_AXIS)); projectPanel.setBorder(BorderFactory.createEmptyBorder(0, 1, 1, 1)); projectPanel.add(radioProjectFiles); projectPanel.add(Box.createHorizontalGlue()); projectPanel.add(projectList); //RadioGroup radiogroup = new ButtonGroup(); radiogroup.add(radioOpenFiles); radiogroup.add(radioDirFiles); radiogroup.add(radioProjectFiles); radiosPanel.add(openBuffersPanel); radiosPanel.add(dirPanel); radiosPanel.add(projectPanel); //LineSeparatorPanel lineSeparatorPanel = new JPanel(new BorderLayout()); String[] ls = { "Unix (\\n)" , "Windows (\\r\\n)" , "Mac (\\r)"}; lineSeparatorCombo = new JComboBox(ls); lineSeparatorCombo.setSelectedIndex(0); lineSeparatorCombo.addActionListener(this); lineSeparatorLabel = new JLabel("Select line separator:"); lineSeparatorPanel.add(lineSeparatorLabel, "West"); lineSeparatorPanel.add(lineSeparatorCombo, "East"); //North Panel northPanel = new JPanel(new BorderLayout()); northPanel.add(lineSeparatorPanel, "North"); northPanel.add(new JSeparator(), "Center"); northPanel.add(radiosPanel, "South"); //Button Panel buttonPanel = new JPanel(new BorderLayout()); cancelButton = new JButton("Cancel"); cancelButton.addActionListener(this); okButton = new JButton("OK"); okButton.addActionListener(this); okButton.setPreferredSize(cancelButton.getPreferredSize()); buttonPanel.add(cancelButton,"East"); buttonPanel.add(okButton,"West"); //Add to main panel content.add(northPanel, "North"); content.add(buttonPanel, "South"); // locate the dialog in the center of the // editing pane and make it visible dialog.getRootPane().setDefaultButton(okButton); dialog.pack(); dialog.setLocationRelativeTo(view); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); //Performs actions void actionPerformed(e) { File selectedDirectory = null; if(e.getSource() == chooserButton) { radiogroup.setSelected(radioDirFiles.getModel(),true); chooserDirectory = new JFileChooser(); chooserDirectory.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int returnVal = chooserDirectory.showOpenDialog(dialog); if (returnVal == JFileChooser.APPROVE_OPTION) { selectedDirectory = chooserDirectory.getSelectedFile(); textDirectory.setText(selectedDirectory.getCanonicalPath()); } return; } else if(e.getSource() == projectList) { radiogroup.setSelected(radioProjectFiles.getModel(),true); return; } else if(e.getSource() == cancelButton) { dialog.dispose(); } else if(e.getSource() == okButton) { lineSeparator = getLineSeparator((String) lineSeparatorCombo.getSelectedItem()); if (radiogroup.isSelected(radioOpenFiles.getModel())) { //Macros.message(fview, "Selected Only Open Files!"); VFSManager.runInWorkThread(new Runnable() { void run() { openBuffers = jEdit.getBuffers(); for (int i = 0; i < openBuffers.length; i++) { buf = openBuffers[i]; setSeparator(buf,lineSeparator); } } }); } else if (radiogroup.isSelected(radioDirFiles.getModel())) { glob = "*.*"; if (selectedDirectory == null) { selectedDirectory = new File(textDirectory.getText()); if (selectedDirectory.canRead() && selectedDirectory.isDirectory()) { vfs = VFSManager.getVFSForPath(textDirectory.getText()); session = vfs.createVFSSession(textDirectory.getText(),dialog); if (filterCheckbox.getModel().isSelected()) { glob = filterTextField.getText(); if (glob == null || glob.length() <= 0) { glob = "*.*"; } } try { files = vfs._listDirectory(session, textDirectory.getText(), glob, true, dialog); } catch (Exception e) { Macros.message(fview, "Not a valid filter!"); return; } vfs._endVFSSession(session,dialog); for (int k = 0; k < files.length; k++) { buf = jEdit.openFile(fview, files[k]); openBuffers = jEdit.getBuffers(); for (int i = 0; i < openBuffers.length; i++) { openBuf = openBuffers[i]; if (buf.getPath().equals(openBuf.getPath())) { closeBuffer = false; } else { closeBuffer = true; } } VFSManager.waitForRequests(); if (buf != null) { setSeparator(buf,lineSeparator); VFSManager.waitForRequests(); if (closeBuffer) { jEdit.closeBuffer(fview, buf); } } } } else { Macros.message(fview, "Not a valid directory!"); return; } } } else { project = projectviewer.ProjectManager.getInstance().getProject(projectList.getSelectedItem()); Collection files = project.getFiles(); for (Iterator it = files.iterator(); it.hasNext();) { file = it.next(); filePath = file.getCanonicalPath(); buf = jEdit.openFile(fview, filePath); openBuffers = jEdit.getBuffers(); for (int i = 0; i < openBuffers.length; i++) { openBuf = openBuffers[i]; if (buf.getPath().equals(openBuf.getPath())) { closeBuffer = false; } else { closeBuffer = true; } } if (buf != null) { VFSManager.runInAWTThread(new Runnable() { void run() { setSeparator(buf,lineSeparator); } }); if (closeBuffer) { jEdit.closeBuffer(fview, buf); } } } } dialog.dispose(); } } } /** * Returns the approapriate line separator * when supplied one of: * 'Unix', 'Windows', anything else is considered 'Mac' */ String getLineSeparator(name) { if (name.indexOf("Unix") != -1) { ls = "\n"; } else if (name.indexOf("Windows") != -1) { ls = "\r\n"; } else { ls = "\r"; } return ls; } /** * Switches the line separator of the supplied buffer to that passed in. */ void setSeparator(buff, separator) { buff.setProperty("lineSeparator", separator); buff.propertiesChanged(); buff.save(view, null); } //Startup showDialog();