1 /* DpocumentFilterDemo.java -- An example for the DocumentFilter class.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 package gnu
.classpath
.examples
.swing
;
25 import java
.awt
.BorderLayout
;
26 import java
.awt
.Color
;
27 import java
.awt
.GridLayout
;
28 import java
.awt
.Toolkit
;
29 import java
.awt
.datatransfer
.Clipboard
;
30 import java
.awt
.datatransfer
.StringSelection
;
31 import java
.awt
.event
.ActionEvent
;
32 import java
.awt
.event
.ActionListener
;
34 import javax
.swing
.JButton
;
35 import javax
.swing
.JComboBox
;
36 import javax
.swing
.JComponent
;
37 import javax
.swing
.JFrame
;
38 import javax
.swing
.JLabel
;
39 import javax
.swing
.JOptionPane
;
40 import javax
.swing
.JPanel
;
41 import javax
.swing
.JTextField
;
42 import javax
.swing
.SwingUtilities
;
43 import javax
.swing
.text
.AbstractDocument
;
44 import javax
.swing
.text
.AttributeSet
;
45 import javax
.swing
.text
.BadLocationException
;
46 import javax
.swing
.text
.DocumentFilter
;
47 import javax
.swing
.text
.TextAction
;
50 * A demonstration of the <code>javax.swing.text.DocumentFilter</code> class.
52 * <p>Similar to a dialog in a popular programming IDE the user can insert
53 * a CVS URL into a textfield and the filter will split the components apart
54 * and will put them into the right textfields saving the user a lot of
57 * @author Robert Schuster
59 public class DocumentFilterDemo
61 implements ActionListener
66 JTextField repositoryPath
;
69 JComboBox connectionType
;
72 * Creates a new demo instance.
74 public DocumentFilterDemo()
77 // initFrameContent() is only called (from main) when running this app
82 * When the demo is run independently, the frame is displayed, so we should
83 * initialise the content panel (including the demo content and a close
84 * button). But when the demo is run as part of the Swing activity board,
85 * only the demo content panel is used, the frame itself is never displayed,
86 * so we can avoid this step.
88 void initFrameContent()
90 JPanel closePanel
= new JPanel();
91 JButton closeButton
= new JButton("Close");
92 closeButton
.setActionCommand("CLOSE");
93 closeButton
.addActionListener(this);
94 closePanel
.add(closeButton
);
95 add(closePanel
, BorderLayout
.SOUTH
);
98 private void createContent()
100 setLayout(new BorderLayout());
102 JPanel panel
= new JPanel(new GridLayout(7, 2));
103 panel
.add(new JLabel("CVS URL:"));
104 panel
.add(target
= new JTextField(20));
105 target
.setBackground(Color
.RED
);
107 panel
.add(new JLabel("Host:"));
108 panel
.add(host
= new JTextField(20));
110 panel
.add(new JLabel("Repository Path:"));
111 panel
.add(repositoryPath
= new JTextField(20));
113 panel
.add(new JLabel("Username:"));
114 panel
.add(user
= new JTextField(20));
116 panel
.add(new JLabel("Password:"));
117 panel
.add(password
= new JTextField(20));
119 panel
.add(new JLabel("Connection Type:"));
120 panel
.add(connectionType
= new JComboBox());
122 JButton helpButton
= new JButton("Help");
123 panel
.add(helpButton
);
125 helpButton
.addActionListener(new ActionListener()
127 public void actionPerformed(ActionEvent ae
)
129 JOptionPane
.showMessageDialog(DocumentFilterDemo
.this,
130 "Paste a CVS URL into the red " +
131 "textfield.\nIf you do not want to " +
132 "look up a CVS URL yourself click " +
133 "on the 'provide me an example' " +
134 "button.\nThis will paste a proper " +
135 "string into your clipboard.");
139 JButton exampleButton
= new JButton("Provide me an example!");
140 panel
.add(exampleButton
);
141 exampleButton
.addActionListener(new ActionListener()
143 public void actionPerformed(ActionEvent ae
)
147 Toolkit tk
= Toolkit
.getDefaultToolkit();
148 Clipboard cb
= tk
.getSystemSelection();
149 StringSelection selection
150 = new StringSelection(":extssh:gnu@cvs.savannah.gnu.org:" +
151 "/cvs/example/project");
153 cb
.setContents(selection
, selection
);
155 // Confirm success with a beep.
158 catch (IllegalStateException ise
)
160 JOptionPane
.showMessageDialog(DocumentFilterDemo
.this,
161 "Clipboard is currently" +
164 JOptionPane
.ERROR_MESSAGE
);
169 connectionType
.addItem("pserver");
170 connectionType
.addItem("ext");
171 connectionType
.addItem("extssh");
175 AbstractDocument doc
= (AbstractDocument
) target
.getDocument();
176 doc
.setDocumentFilter(new CVSFilter());
179 public void actionPerformed(ActionEvent e
)
181 if (e
.getActionCommand().equals("CLOSE"))
186 public static void main(String
[] args
)
188 SwingUtilities
.invokeLater
193 DocumentFilterDemo app
= new DocumentFilterDemo();
194 app
.initFrameContent();
195 JFrame frame
= new JFrame("DocumentFilterDemo");
196 frame
.getContentPane().add(app
);
198 frame
.setVisible(true);
199 frame
.setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
205 * Returns a DemoFactory that creates a DocumentFilterDemo.
207 * @return a DemoFactory that creates a DocumentFilterDemo
209 public static DemoFactory
createDemoFactory()
211 return new DemoFactory()
213 public JComponent
createDemo()
215 return new DocumentFilterDemo();
220 class CVSFilter
extends DocumentFilter
222 // example: pserver:anonymous@cvs.sourceforge.net:/cvsroot/fmj
223 String cvsPattern
= ":?(pserver|ext|extssh):(\\w)+(:(\\w)+)?@(\\w|\\.)+:/(\\w|/)*";
225 public void insertString(DocumentFilter
.FilterBypass fb
,
226 int offset
, String string
,
228 throws BadLocationException
230 filterString(fb
, offset
, 0, string
, attr
, true);
233 public void replace(DocumentFilter
.FilterBypass fb
,
234 int offset
, int length
,
237 throws BadLocationException
239 filterString(fb
, offset
, length
, string
, attr
, false);
242 public void filterString(DocumentFilter
.FilterBypass fb
,
243 int offset
, int length
, String string
,
244 AttributeSet attr
, boolean insertion
)
245 throws BadLocationException
247 if(string
.matches(cvsPattern
))
249 // Split off the connection type part.
250 String
[] result
= string
.split(":", 2);
252 // If the string contained a leading colon, result[0]
253 // will be empty at that point. We simply repeat the split
254 // operation on the remaining string and continue.
255 if(result
[0].equals(""))
256 result
= result
[1].split(":", 2);
258 connectionType
.setSelectedItem(result
[0]);
260 // Split off the username and password part
261 result
= result
[1].split("@", 2);
263 // Break username and password in half
264 String
[] userCredentials
= result
[0].split(":");
265 user
.setText(userCredentials
[0]);
267 // If the result has two entries the second one will
269 if (userCredentials
.length
== 2)
270 password
.setText(userCredentials
[1]);
272 // Now break the host part apart.
273 result
= result
[1].split(":");
275 host
.setText(result
[0]);
277 repositoryPath
.setText(result
[1]);
280 // The unmodified string is put into the document.
282 fb
.insertString(offset
, string
, attr
);
284 fb
.replace(offset
, length
, string
, attr
);