public void actionPerformed(ActionEvent e) { // Handle open button action. if (e.getSource() == openButton) { int returnVal = fc.showOpenDialog(FileChooserDemo.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); // This is where a real application would open the file. log.append("Opening: " + file.getName() + "." + newline); } else { log.append("Open command cancelled by user." + newline); } log.setCaretPosition(log.getDocument().getLength()); // Handle save button action. } else if (e.getSource() == saveButton) { int returnVal = fc.showSaveDialog(FileChooserDemo.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); // This is where a real application would save the file. log.append("Saving: " + file.getName() + "." + newline); } else { log.append("Save command cancelled by user." + newline); } log.setCaretPosition(log.getDocument().getLength()); } }
public DistributedTextEditor() { area1.setFont(new Font("Monospaced", Font.PLAIN, 12)); area2.setFont(new Font("Monospaced", Font.PLAIN, 12)); ((AbstractDocument) area1.getDocument()).setDocumentFilter(dec); area2.setEditable(false); Container content = getContentPane(); content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); JScrollPane scroll1 = new JScrollPane( area1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); content.add(scroll1, BorderLayout.CENTER); JScrollPane scroll2 = new JScrollPane( area2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); content.add(scroll2, BorderLayout.CENTER); content.add(ipaddress, BorderLayout.CENTER); content.add(portNumber, BorderLayout.CENTER); JMenuBar JMB = new JMenuBar(); setJMenuBar(JMB); JMenu file = new JMenu("File"); JMenu edit = new JMenu("Edit"); JMB.add(file); JMB.add(edit); file.add(Listen); file.add(Connect); file.add(Disconnect); file.addSeparator(); file.add(Save); file.add(SaveAs); file.add(Quit); edit.add(Copy); edit.add(Paste); edit.getItem(0).setText("Copy"); edit.getItem(1).setText("Paste"); Save.setEnabled(false); SaveAs.setEnabled(false); Disconnect.setEnabled(false); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); area1.addKeyListener(k1); area1.addMouseListener(m1); setTitle("Disconnected"); setVisible(true); area1.insert("Welcome to Hjortehandlerne's distributed text editor. \n", 0); this.addWindowListener(w1); }
public void actionPerformed(ActionEvent e) { // Handle open button action. if (e.getSource() == openButton) { int returnVal = fc.showOpenDialog(DirectorySize.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); // This is where a real application would open the file. ListSubDirectorySizes(file); } else { log.append("Open command cancelled by user." + newline); } log.setCaretPosition(log.getDocument().getLength()); // Handle save button action. } else if (e.getSource() == saveButton) { log.setText(""); // reset } }
EditFrame(RopeFrame parent) { super(parent); // Implement a smarter way to set the initial frame position and size setLocation(0, 0); setSize(670, 705); try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } sourceArea.addCaretListener(this); browseButton.addActionListener(this); optionsButton.addActionListener(this); assembleButton.addActionListener(this); saveButton.addActionListener(this); messageList.addMouseListener( new MouseAdapter() { @Override public void mouseClicked(MouseEvent event) { highlightError(messageList.locationToIndex(event.getPoint())); } }); undoMgr = new CompoundUndoManager(sourceArea); undoAction = undoMgr.getUndoAction(); redoAction = undoMgr.getRedoAction(); undoMgr.updateUndoAction = new UpdateUndoAction(); undoMgr.updateRedoAction = new UpdateRedoAction(); document = sourceArea.getDocument(); ActionMap am = sourceArea.getActionMap(); InputMap im = sourceArea.getInputMap(JComponent.WHEN_FOCUSED); // Remove automatic key bindings because we want them controlled by menu items im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, RopeHelper.modifierMaks), "none"); im.put( KeyStroke.getKeyStroke(KeyEvent.VK_Z, RopeHelper.modifierMaks + InputEvent.SHIFT_MASK), "none"); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, RopeHelper.modifierMaks), "none"); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, RopeHelper.modifierMaks), "none"); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, RopeHelper.modifierMaks), "none"); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, RopeHelper.modifierMaks), "none"); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_L, RopeHelper.modifierMaks), "none"); // Set custom binding action for tab key String action = "tabKeyAction"; im.put(KeyStroke.getKeyStroke("TAB"), action); am.put( action, new AbstractAction() { private static final long serialVersionUID = 1L; @Override public void actionPerformed(ActionEvent e) { try { int caretPos = sourceArea.getCaretPosition(); int lineNum = sourceArea.getLineOfOffset(caretPos); int startLine = sourceArea.getLineStartOffset(lineNum); int endLine = sourceArea.getLineEndOffset(lineNum); int linePos = caretPos - startLine; if (linePos >= 39 && linePos < 79) { caretPos = startLine + linePos + 10 - ((linePos + 1) % 10); } else if (linePos >= 20 && linePos <= 39) { caretPos = startLine + 39; } else if (linePos >= 15 && linePos <= 19) { caretPos = startLine + 20; } else if (linePos >= 5 && linePos <= 14) { caretPos = startLine + 15; } else { caretPos = startLine + 5; } // If the line is shorter than the new position fo the caret add enough spaces... if (caretPos > endLine) { StringBuilder str = new StringBuilder(); int size = caretPos - endLine; while (size-- >= 0) { str.append(' '); } document.insertString(endLine - 1, str.toString(), null); } sourceArea.setCaretPosition(caretPos); } catch (BadLocationException ex) { ex.printStackTrace(); } } }); // Set custom binding action for return/enter key String actionKey = "backspaceKeyAction"; im.put(KeyStroke.getKeyStroke("BACK_SPACE"), actionKey); am.put( actionKey, new AbstractAction() // How can I get the original action? { private static final long serialVersionUID = 1L; @Override public void actionPerformed(ActionEvent e) { try { int caretPos = sourceArea.getCaretPosition(); int lineNum = sourceArea.getLineOfOffset(caretPos); int startLine = sourceArea.getLineStartOffset(lineNum); int endLine = sourceArea.getLineEndOffset(lineNum); int linePos = caretPos - startLine; if (linePos == 15) { int endPos = 5; int charPos = linePos; for (; charPos > endPos; charPos--) { char ch = sourceArea.getText().charAt((startLine + charPos) - 1); if (!Character.isWhitespace(ch)) { break; } } sourceArea.setCaretPosition(startLine + charPos); } else { int startSel = sourceArea.getSelectionStart(); int endSel = sourceArea.getSelectionEnd(); if (startSel == endSel) { startSel = caretPos - 1; endSel = caretPos; } StringBuilder sb = new StringBuilder(sourceArea.getText()); sb.replace(startSel, endSel, ""); sourceArea.setText(sb.toString()); sourceArea.setCaretPosition(startSel); } } catch (BadLocationException ex) { ex.printStackTrace(); } } }); // Set custom binding action for return/enter key action = "enterKeyAction"; im.put(KeyStroke.getKeyStroke("ENTER"), action); am.put( action, new AbstractAction() { private static final long serialVersionUID = 1L; @Override public void actionPerformed(ActionEvent e) { try { int caretPos = sourceArea.getCaretPosition(); int lineNum = sourceArea.getLineOfOffset(caretPos); int startLine = sourceArea.getLineStartOffset(lineNum); int linePos = caretPos - startLine; if (linePos >= 5) { document.insertString(caretPos, "\n ", null); } else { document.insertString(caretPos, "\n", null); } } catch (BadLocationException ex) { ex.printStackTrace(); } } }); document.addDocumentListener( new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { setSourceChanged(true); } @Override public void removeUpdate(DocumentEvent e) { setSourceChanged(true); } @Override public void changedUpdate(DocumentEvent e) { setSourceChanged(true); } }); }
/* Add a message to the message area, auto-scroll to end */ public synchronized void message(String s) { mssgArea.append(s + "\n"); mssgArea.setCaretPosition(mssgArea.getDocument().getLength()); }
public void setDocumentFilter(DocumentFilter filter) { ((AbstractDocument) area1.getDocument()).setDocumentFilter(filter); }
public Viewer() { leveldbStore.setMultiSelectionEnabled(false); leveldbStore.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); putButton.setEnabled(false); key.setEnabled(false); value.setEnabled(false); findField.setEnabled(false); deleteButton.setEnabled(false); saveButton.setEnabled(false); putType.setEnabled(false); putType.setEditable(false); signedBox.setEnabled(false); openButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (openButton.isEnabled()) { openButton.setEnabled(false); new Thread() { public void run() { if (leveldbStore.showOpenDialog(pane) == JFileChooser.APPROVE_OPTION) { File select = leveldbStore.getSelectedFile(); if (select.isDirectory()) { new OpenLevelDBDialog(Viewer.this, select); openDatabase(select); dbPathField.setText(select.getAbsolutePath()); } else { JOptionPane.showMessageDialog( pane, "The selecting item must be a directory", "Unable to load database", JOptionPane.WARNING_MESSAGE); } } else { openButton.setEnabled(true); } } }.start(); } } }); deleteButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (dataList.getSelectedValue() != null) { delete(dataList.getSelectedValue().key); } openDatabase(leveldbStore.getSelectedFile()); } }); putButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { put( ((PutType) putType.getSelectedItem()).getBytes(key.getText()), ((PutType) putType.getSelectedItem()).getBytes(value.getText())); openDatabase(leveldbStore.getSelectedFile()); } }); findField.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { openDatabase(leveldbStore.getSelectedFile()); } }); findField .getDocument() .addDocumentListener( new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { openDatabase(leveldbStore.getSelectedFile()); } @Override public void removeUpdate(DocumentEvent e) { openDatabase(leveldbStore.getSelectedFile()); } @Override public void changedUpdate(DocumentEvent e) { openDatabase(leveldbStore.getSelectedFile()); } }); findField .getDocument() .addUndoableEditListener( new UndoableEditListener() { @Override public void undoableEditHappened(UndoableEditEvent e) { openDatabase(leveldbStore.getSelectedFile()); } }); hexKey .getDocument() .addDocumentListener( new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { update(hexKey); } @Override public void removeUpdate(DocumentEvent e) { update(hexKey); } @Override public void changedUpdate(DocumentEvent e) { update(hexKey); } }); hexKey .getDocument() .addUndoableEditListener( new UndoableEditListener() { @Override public void undoableEditHappened(UndoableEditEvent e) { update(hexKey); } }); stringKey .getDocument() .addDocumentListener( new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { update(stringKey); } @Override public void removeUpdate(DocumentEvent e) { update(stringKey); } @Override public void changedUpdate(DocumentEvent e) { update(stringKey); } }); stringKey .getDocument() .addUndoableEditListener( new UndoableEditListener() { @Override public void undoableEditHappened(UndoableEditEvent e) { update(stringKey); } }); hexValue .getDocument() .addDocumentListener( new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { update(hexValue); } @Override public void removeUpdate(DocumentEvent e) { update(hexValue); } @Override public void changedUpdate(DocumentEvent e) { update(hexValue); } }); hexValue .getDocument() .addUndoableEditListener( new UndoableEditListener() { @Override public void undoableEditHappened(UndoableEditEvent e) { update(hexValue); } }); stringValue .getDocument() .addDocumentListener( new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { update(stringValue); } @Override public void removeUpdate(DocumentEvent e) { update(stringValue); } @Override public void changedUpdate(DocumentEvent e) { update(stringValue); } }); stringValue .getDocument() .addUndoableEditListener( new UndoableEditListener() { @Override public void undoableEditHappened(UndoableEditEvent e) { update(stringValue); } }); saveButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { save(); openDatabase(leveldbStore.getSelectedFile()); } }); dataList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); dataList.addListSelectionListener( new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { DBItem item = dataList.getSelectedValue(); if (item != null) { hexValue.setText(cutToLine(LevelDBViewer.toHexString(item.value), 64)); stringValue.setText(cutToLine(new String(item.value), 64)); hexKey.setText(cutToLine(LevelDBViewer.toHexString(item.key), 64)); stringKey.setText(cutToLine(new String(item.key), 64)); lengthLabel.setText(String.valueOf(item.value.length + item.key.length)); keyLength.setText(String.valueOf(item.key.length)); valueLength.setText(String.valueOf(item.value.length)); } } }); signedBox.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { LevelDBViewer.DEFAULT_SINGED = signedBox.isSelected(); int i = dataList.getSelectedIndex(); dataList.clearSelection(); dataList.updateUI(); dataList.setSelectedIndex(i); update(hexKey); update(hexValue); } }); for (PutType t : PutType.values()) { putType.addItem(t); } putType.setSelectedItem(PutType.STRING); putType.addItemListener( new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { openDatabase(leveldbStore.getSelectedFile()); } }); putType.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { openDatabase(leveldbStore.getSelectedFile()); } }); dialog.setLocationByPlatform(true); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setContentPane(pane); dialog.setTitle("LevelDB Viewer By Marcus (https://github.com/SuperMarcus)"); dialog.getRootPane().setDefaultButton(openButton); dialog.pack(); dialog.setVisible(true); }
private void appendMessage(String message) { textArea.append(message); textArea.setCaretPosition(textArea.getDocument().getLength()); }