下面列出了怎么用javax.swing.text.DefaultEditorKit的API类实例代码及写法,或者点击链接到github查看源代码。
public void testPlainEditorKits() {
// VIS: JEditorPane when constructed contains javax.swing.JEditorPane$PlainEditorKit
// and calling JEP.setContenetType("text/plain") has no effect. IMO this is probably
// a defect in JDK, becuase JEP should always honour its EditorKit registry.
JEditorPane pane = new JEditorPane();
pane.setEditorKit(new DefaultEditorKit() {
public @Override String getContentType() {
return "text/whatever";
}
});
setContentTypeInAwt(pane, "text/plain");
// Test JDK kit
EditorKit kitFromJdk = pane.getEditorKit();
assertNotNull("Can't find JDK kit for text/plain", kitFromJdk);
assertEquals("The kit for text/plain should not be from JDK",
"org.netbeans.modules.editor.plain.PlainKit", kitFromJdk.getClass().getName());
// Test Netbeans kit
EditorKit kitFromNb = CloneableEditorSupport.getEditorKit("text/plain");
assertNotNull("Can't find Nb kit for text/plain", kitFromNb);
assertEquals("Wrong Nb kit for text/plain",
"org.netbeans.modules.editor.plain.PlainKit", kitFromNb.getClass().getName());
}
public void testNewlineLineOne() throws Exception {
loggingOn();
RandomTestContainer container = createContainer();
JEditorPane pane = container.getInstance(JEditorPane.class);
Document doc = pane.getDocument();
doc.putProperty("mimeType", "text/plain");
ViewHierarchyRandomTesting.initRandomText(container);
ViewHierarchyRandomTesting.addRound(container).setOpCount(OP_COUNT);
ViewHierarchyRandomTesting.testFixedScenarios(container);
RandomTestContainer.Context context = container.context();
// Clear document contents
DocumentTesting.remove(context, 0, doc.getLength());
DocumentTesting.insert(context, 0, "\n");
EditorPaneTesting.moveOrSelect(context, SwingConstants.NORTH, false);
EditorPaneTesting.moveOrSelect(context, SwingConstants.SOUTH, true);
EditorPaneTesting.performAction(context, pane, DefaultEditorKit.deleteNextCharAction);
DocumentTesting.undo(context, 1);
}
private void updateActionMap(Selection newSelection) {
ActionMap actionMap = getActionMap();
actionMap.put(SelectionActions.SELECT_ALL, new SelectAllAction());
actionMap.put(DefaultEditorKit.pasteAction, new PasteAction());
if (!newSelection.isEmpty()) {
actionMap.put(DefaultEditorKit.cutAction, new CutAction());
actionMap.put(DefaultEditorKit.copyAction, new CopyAction());
actionMap.put("delete", new DeleteAction());
actionMap.put(SelectionActions.DESELECT_ALL, new DeselectAllAction());
} else {
actionMap.remove(DefaultEditorKit.cutAction);
actionMap.remove(DefaultEditorKit.copyAction);
actionMap.remove("delete");
actionMap.remove(SelectionActions.DESELECT_ALL);
}
getDynamicContent().remove(actionMap);
getDynamicContent().add(actionMap);
}
public void testTwoDeletes() throws Exception {
loggingOn();
RandomTestContainer container = createContainer();
JEditorPane pane = container.getInstance(JEditorPane.class);
Document doc = pane.getDocument();
doc.putProperty("mimeType", "text/plain");
ViewHierarchyRandomTesting.initRandomText(container);
RandomTestContainer.Context context = container.context();
DocumentTesting.insert(context, 0,
" osen \n\n\nbs\tmn\nziil esl\t\ta \t\t \n\n\nabc \n\n\nd\td m\t\ta\nbcdef\te\t\tab\tcdef\tef\tkojd \t\t \n\n\net\t vpjm\ta\ngooywzmj q\tugos\tdefy\t i xs us ttl\tg z"
);
EditorPaneTesting.setCaretOffset(context, 115);
EditorPaneTesting.performAction(context, pane, DefaultEditorKit.deletePrevCharAction);
EditorPaneTesting.performAction(context, pane, DefaultEditorKit.deleteNextCharAction);
}
public void testSelectionAndInsertTab() throws Exception {
loggingOn();
RandomTestContainer container = createContainer();
JEditorPane pane = container.getInstance(JEditorPane.class);
Document doc = pane.getDocument();
doc.putProperty("mimeType", "text/plain");
ViewHierarchyRandomTesting.initRandomText(container);
RandomTestContainer.Context context = container.context();
DocumentTesting.insert(context, 0,
" osen \n\n\n esl\t\ta \t\t \n\n\nabcd\td m\t\tabcdef\te\t\tab\tcdef\tef\tkojd p\t\t \n\n\n t\t vpjm\ta\ngooywzmj q\tugos\tdefy\t i xs us tg z"
);
EditorPaneTesting.setCaretOffset(context, 64);
EditorPaneTesting.moveOrSelect(context, SwingConstants.NORTH, false);
EditorPaneTesting.moveOrSelect(context, SwingConstants.SOUTH, true);
DocumentTesting.insert(context, 19, "g");
EditorPaneTesting.moveOrSelect(context, SwingConstants.EAST, true);
EditorPaneTesting.moveOrSelect(context, SwingConstants.NORTH, true);
EditorPaneTesting.moveOrSelect(context, SwingConstants.WEST, true);
EditorPaneTesting.performAction(context, pane, DefaultEditorKit.deletePrevCharAction);
EditorPaneTesting.performAction(context, pane, DefaultEditorKit.insertTabAction);
EditorPaneTesting.moveOrSelect(context, SwingConstants.EAST, false);
EditorPaneTesting.typeChar(context, 'f');
}
private void installNoOpActionMappings(){
InputMap im = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
// cut
KeyStroke[] keys = findEditorKeys(DefaultEditorKit.cutAction, KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
for (int i = 0; i < keys.length; i++) {
im.put(keys[i], NOOP_ACTION_KEY);
}
// copy
keys = findEditorKeys(DefaultEditorKit.copyAction, KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
for (int i = 0; i < keys.length; i++) {
im.put(keys[i], NOOP_ACTION_KEY);
}
// delete
keys = findEditorKeys(DefaultEditorKit.deleteNextCharAction, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); //NOI18N
for (int i = 0; i < keys.length; i++) {
im.put(keys[i], NOOP_ACTION_KEY);
}
// paste
keys = findEditorKeys(DefaultEditorKit.pasteAction, KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
for (int i = 0; i < keys.length; i++) {
im.put(keys[i], NOOP_ACTION_KEY);
}
getActionMap().put(NOOP_ACTION_KEY, NOOP_ACTION);
}
@Nonnull
@ReturnsOriginal
public static JPopupMenu addTextActions(@Nonnull final JPopupMenu menu) {
final Action cut = new DefaultEditorKit.CutAction();
cut.putValue(Action.NAME, "Cut");
menu.add(cut);
final Action copy = new DefaultEditorKit.CopyAction();
copy.putValue(Action.NAME, "Copy");
menu.add(copy);
final Action paste = new DefaultEditorKit.PasteAction();
paste.putValue(Action.NAME, "Paste");
menu.add(paste);
menu.add(new SelectAllTextAction());
return menu;
}
static WebPopupMenu createCopyMenu(boolean modifiable) {
WebPopupMenu menu = new WebPopupMenu();
if (modifiable) {
Action cut = new DefaultEditorKit.CutAction();
cut.putValue(Action.NAME, Tr.tr("Cut"));
cut.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control X"));
menu.add(cut);
}
Action copy = new DefaultEditorKit.CopyAction();
copy.putValue(Action.NAME, Tr.tr("Copy"));
copy.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control C"));
menu.add(copy);
if (modifiable) {
Action paste = new DefaultEditorKit.PasteAction();
paste.putValue(Action.NAME, Tr.tr("Paste"));
paste.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control V"));
menu.add(paste);
}
return menu;
}
/**
* Inits the.
*
* @param file the file
* @param d the d
*/
public void init(File file, Dimension d) {
createMenus();
this.logFile = file;
this.textArea = new JTextArea();
// Copy
Action copyAction = this.textArea.getActionMap().get(DefaultEditorKit.copyAction);
copyAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));
copyAction.setEnabled(true);
this.scrollPane = new JScrollPane(this.textArea);
this.setContentPane(this.scrollPane);
this.scrollPane.setPreferredSize(d);
boolean doneLoadingFile = loadFile();
if (!doneLoadingFile) {
this.dispose();
return;
}
this.pack();
this.setVisible(true);
}
private static JMenuBar createMenuBar() {
Component edit = makeEditButtonBar(Arrays.asList(
makeButton("Cut", new DefaultEditorKit.CutAction()),
makeButton("Copy", new DefaultEditorKit.CopyAction()),
makeButton("Paste", new DefaultEditorKit.PasteAction())));
JMenu menu = new JMenu("File");
menu.add("111111111");
menu.addSeparator();
menu.add(makeEditMenuItem(edit));
menu.addSeparator();
menu.add("22222");
menu.add("3333");
menu.add("4444444");
JMenuBar mb = new JMenuBar();
mb.add(menu);
return mb;
}
private ServicesTab() {
manager = new ExplorerManager();
manager.setRootContext(new ServicesNode());
ActionMap map = getActionMap();
map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
map.put("delete", ExplorerUtils.actionDelete(manager, false));
associateLookup(ExplorerUtils.createLookup(manager, map));
view = new BeanTreeView();
view.setRootVisible(false);
setLayout(new BorderLayout());
add(view);
setName(ID);
setDisplayName(NbBundle.getMessage(ServicesTab.class, "LBL_Services"));
}
private JMenu createEditMenu()
{
JMenu menu = new JMenu();
menu.setText(LanguageBundle.getString("in_mnuEdit"));
menu.setMnemonic(KeyEvent.VK_E);
menu.add(new JMenuItem(actionMap.get(PCGenActionMap.ADD_KIT_COMMAND)));
menu.addSeparator();
menu.add(tempMenu);
menu.addSeparator();
JMenuItem cutMenuItem = new JMenuItem(new DefaultEditorKit.CutAction());
cutMenuItem.setText("Cut");
cutMenuItem.setMnemonic(KeyEvent.VK_T);
menu.add(cutMenuItem);
JMenuItem copyMenuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
copyMenuItem.setText("Copy");
copyMenuItem.setMnemonic(KeyEvent.VK_C);
menu.add(copyMenuItem);
JMenuItem pasteMenuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
pasteMenuItem.setText("Paste");
pasteMenuItem.setMnemonic(KeyEvent.VK_P);
menu.add(pasteMenuItem);
return menu;
}
/**
* This method builds the popup menu.
*/
private void build(ActionMap actionMap) {
popupPresenter = new JPopupMenu();
cutAction = actionMap.get(DefaultEditorKit.cutAction);
copyAction = actionMap.get(DefaultEditorKit.copyAction);
pasteAction = actionMap.get(DefaultEditorKit.pasteAction);
selectAllAction = actionMap.get(DefaultEditorKit.selectAllAction);
popupPresenter.add(createMenuItem("CTL_MenuItem_Cut", KeyEvent.VK_X, cutAction));
popupPresenter.add(createMenuItem("CTL_MenuItem_Copy", KeyEvent.VK_C, copyAction));
popupPresenter.add(createMenuItem("CTL_MenuItem_Paste", KeyEvent.VK_V, pasteAction));
popupPresenter.addSeparator();
popupPresenter.add(createMenuItem("CTL_MenuItem_SelectAll", KeyEvent.VK_A, selectAllAction));
}
/** Creates new form CatalogPanel */
public CatalogPanel() {
ActionMap map = getActionMap();
map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(getExplorerManager()));
map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(getExplorerManager()));
map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(getExplorerManager()));
map.put("delete", ExplorerUtils.actionDelete(getExplorerManager(), true)); // NOI18N
initComponents();
createCatalogView();
treePanel.add(view, BorderLayout.CENTER);
associateLookup(ExplorerUtils.createLookup(getExplorerManager(), map));
initialize();
}
public static void typeChar(Context context, char ch) throws Exception {
JEditorPane pane = context.getInstance(JEditorPane.class);
if (ch == '\n') { // Insert break
performAction(context, pane, DefaultEditorKit.insertBreakAction, null, true);
} else if (ch == '\t') { // Insert TAB
performAction(context, pane, DefaultEditorKit.insertTabAction, null, true);
} else { // default key typed action
StringBuilder sb = null;
if (context.isLogOp()) {
sb = context.logOpBuilder();
sb.append("typeChar(context, '").append(CharSequenceUtilities.debugChar(ch)).append("')\n");
debugCaret(sb, pane).append(" => ");
}
performAction(context, pane, DefaultEditorKit.defaultKeyTypedAction,
new ActionEvent(pane, 0, String.valueOf(ch)), false);
if (context.isLogOp()) {
debugCaret(sb, pane).append("\n");
context.logOp(sb);
}
}
}
@Override
protected void installKeyboardActions() {
super.installKeyboardActions();
ActionMap map = getComponent().getActionMap();
if (map.get(DefaultEditorKit.selectWordAction) != null) {
Action a = map.get(DefaultEditorKit.selectLineAction);
if (a != null) {
map.remove(DefaultEditorKit.selectWordAction);
map.put(DefaultEditorKit.selectWordAction, a);
}
}
}
public TestFormattedTextField(InternationalFormatter fmt) {
super(fmt);
fmt.setAllowsInvalid(false);
fmt.setOverwriteMode(true);
backspace = getActionMap().get(DefaultEditorKit.deletePrevCharAction);
delete = getActionMap().get(DefaultEditorKit.deleteNextCharAction);
insert = getActionMap().get(DefaultEditorKit.insertContentAction);
dummyEvent = new ActionEvent(this, 0, null);
}
public TestFormattedTextField(InternationalFormatter fmt) {
super(fmt);
fmt.setAllowsInvalid(false);
fmt.setOverwriteMode(true);
backspace = getActionMap().get(DefaultEditorKit.deletePrevCharAction);
delete = getActionMap().get(DefaultEditorKit.deleteNextCharAction);
insert = getActionMap().get(DefaultEditorKit.insertContentAction);
dummyEvent = new ActionEvent(this, 0, null);
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
public TestFormattedTextField(InternationalFormatter fmt) {
super(fmt);
fmt.setAllowsInvalid(false);
fmt.setOverwriteMode(true);
backspace = getActionMap().get(DefaultEditorKit.deletePrevCharAction);
delete = getActionMap().get(DefaultEditorKit.deleteNextCharAction);
insert = getActionMap().get(DefaultEditorKit.insertContentAction);
dummyEvent = new ActionEvent(this, 0, null);
}
public TestFormattedTextField(InternationalFormatter fmt) {
super(fmt);
fmt.setAllowsInvalid(false);
fmt.setOverwriteMode(true);
backspace = getActionMap().get(DefaultEditorKit.deletePrevCharAction);
delete = getActionMap().get(DefaultEditorKit.deleteNextCharAction);
insert = getActionMap().get(DefaultEditorKit.insertContentAction);
dummyEvent = new ActionEvent(this, 0, null);
}
public TestFormattedTextField(InternationalFormatter fmt) {
super(fmt);
fmt.setAllowsInvalid(false);
fmt.setOverwriteMode(true);
backspace = getActionMap().get(DefaultEditorKit.deletePrevCharAction);
delete = getActionMap().get(DefaultEditorKit.deleteNextCharAction);
insert = getActionMap().get(DefaultEditorKit.insertContentAction);
dummyEvent = new ActionEvent(this, 0, null);
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
public static void setDefaultLookAndFeel() {
String laf = UIManager.getCrossPlatformLookAndFeelClassName();
InputMap textFieldInputMap = (InputMap) UIManager.get("TextField.focusInputMap");
InputMap textAreaInputMap = (InputMap) UIManager.get("TextArea.focusInputMap");
setLookAndFeel(laf);
if (DesktopApi.getOs().isMac()) {
textFieldInputMap.put(KeyStroke.getKeyStroke("UP"), DefaultEditorKit.upAction);
textAreaInputMap.put(KeyStroke.getKeyStroke("UP"), DefaultEditorKit.upAction);
textFieldInputMap.put(KeyStroke.getKeyStroke("DOWN"), DefaultEditorKit.downAction);
textAreaInputMap.put(KeyStroke.getKeyStroke("DOWN"), DefaultEditorKit.downAction);
UIManager.put("TextField.focusInputMap", textFieldInputMap);
UIManager.put("TextArea.focusInputMap", textAreaInputMap);
}
}
private static void checkArray(JTextComponent.KeyBinding[] keyActionArray) {
if (keyActionArray.length != 1) {
throw new RuntimeException("Wrong array lenght!");
}
if (!DefaultEditorKit.upAction.equals(keyActionArray[0].actionName)) {
throw new RuntimeException("Wrong action name!");
}
if (!KeyStroke.getKeyStroke("UP").equals(keyActionArray[0].key)) {
throw new RuntimeException("Wrong keystroke!");
}
}
public TestFormattedTextField(InternationalFormatter fmt) {
super(fmt);
fmt.setAllowsInvalid(false);
fmt.setOverwriteMode(true);
backspace = getActionMap().get(DefaultEditorKit.deletePrevCharAction);
delete = getActionMap().get(DefaultEditorKit.deleteNextCharAction);
insert = getActionMap().get(DefaultEditorKit.insertContentAction);
dummyEvent = new ActionEvent(this, 0, null);
}
final javax.swing.ActionMap setupActionMap(javax.swing.ActionMap map) {
map.put(DefaultEditorKit.copyAction, copyActionPerformer);
map.put(DefaultEditorKit.cutAction, cutActionPerformer);
//map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(explorerManager));
map.put("delete", deleteActionPerformer); // NOI18N
return map;
}
public ExpPanel() {
manager = new ExplorerManager();
ActionMap map = getActionMap();
map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
map.put("delete", ExplorerUtils.actionDelete(manager, true));
lookup = ExplorerUtils.createLookup(manager, map);
InputMap keys = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
keys.put(KeyStroke.getKeyStroke("control c"), DefaultEditorKit.copyAction);
keys.put(KeyStroke.getKeyStroke("control x"), DefaultEditorKit.cutAction);
keys.put(KeyStroke.getKeyStroke("control v"), DefaultEditorKit.pasteAction);
keys.put(KeyStroke.getKeyStroke("DELETE"), "delete");
}
/**
* Constructor. Sets up the styles and add the strings to be highlighted into the corresponding
* vectors.
*/
public ConsoleCodeDocument() {
super(false);
putProperty(DefaultEditorKit.EndOfLineStringProperty, "\n");
StyleConstants.setForeground(pythonPromptAttr, Color.LIGHT_GRAY);
StyleConstants.setBold(pythonPromptAttr, true);
StyleConstants.setFontSize(pythonPromptAttr, 13);
}
public void testGlobalChanges() throws Exception {
class MyAction extends AbstractAction {
public int cntEnabled;
public int cntPerformed;
public boolean isEnabled() {
cntEnabled++;
return true;
}
public void actionPerformed(ActionEvent ev) {
cntPerformed++;
}
}
MyAction myAction = new MyAction();
ActionMap tc = new ActionMap();
tc.put(DefaultEditorKit.copyAction, myAction);
CopyAction a = (CopyAction)CopyAction.get(CopyAction.class);
ActionsInfraHid.setActionMap(tc);
try {
assertTrue("MyAction is enabled", a.isEnabled());
assertEquals("isEnabled called once", 1, myAction.cntEnabled);
a.setActionPerformer(null);
assertEquals("An enabled is currentlly called again", 2, myAction.cntEnabled);
} finally {
ActionsInfraHid.setActionMap(null);
}
}