/** * Returns a JTextArea in a scroll pane * * @return JTextArea in a scroll pane */ protected JComponent getCenterComponent() { // visually placed in the south ! lineCounter = new LineCounterField(); lineCounter.setBorder(BorderFactory.createEmptyBorder(0, SwingConfig.PADDING, 0, 0)); // end of visually placed in the south saveAction = new SaveAction(getLocalizer().localize("button.save")); // text area textArea = new JTextArea(); textArea.setLineWrap(false); textArea.setEditable(true); textArea.addCaretListener(lineCounter); textArea .getKeymap() .addActionForKeyStroke( KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK), saveAction); loadFile(); JScrollPane contentScrollPane = new JScrollPane(textArea); contentScrollPane.setAlignmentX(Component.LEFT_ALIGNMENT); Rectangle screen = getGraphicsConfiguration().getBounds(); contentScrollPane.setPreferredSize( new Dimension(Math.min(screen.width / 3, 420), Math.min(2 * screen.height / 3, 560))); return contentScrollPane; }
public void install(JTextArea textArea) { textArea.addCaretListener(this); textArea.addComponentListener(this); textArea.addFocusListener(this); textArea.addKeyListener(this); textArea.addMouseListener(this); textArea.addMouseMotionListener(this); }
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); } }); }
private JPanel editMarketingPanel(final JPanel marketPanel, int ID) { BorderLayout marketNoteLayout = new BorderLayout(); marketNoteLayout.setVgap(10); marketPanel.setLayout(marketNoteLayout); JLabel noteDateLabel = new JLabel("Note Date: "); noteDateLabel.setFont(new Font("Tahoma", Font.BOLD, 20)); final JLabel noteDate = new JLabel(getNoteDate(2, ID)); noteDate.setFont(new Font("Tahoma", Font.PLAIN, 20)); JLabel machineIDLabel = new JLabel("Machine ID: "); machineIDLabel.setFont(new Font("Tahoma", Font.BOLD, 20)); JLabel machineID = new JLabel(String.valueOf(ID + 1)); machineID.setVerticalTextPosition(SwingConstants.BOTTOM); machineID.setFont(new Font("Tahoma", Font.PLAIN, 20)); JLabel header = new JLabel("MARKETING NOTE"); header.setFont(new Font("Verdana", Font.BOLD, 25)); header.setHorizontalAlignment(SwingConstants.CENTER); JPanel topGrid = new JPanel(new GridLayout(2, 1)); JPanel dateFlow = new JPanel(new FlowLayout()); JPanel idFlow = new JPanel(new FlowLayout()); JPanel dateIdGrid = new JPanel(new GridLayout(1, 2)); dateFlow.add(noteDateLabel); dateFlow.add(noteDate); idFlow.add(machineIDLabel); idFlow.add(machineID); topGrid.add(header); dateIdGrid.add(dateFlow); dateIdGrid.add(idFlow); topGrid.add(dateIdGrid); // topFlow.add(topGrid); // text field for the restocker note JLabel noteLabel = new JLabel("Note: "); noteLabel.setFont(new Font("Tahoma", Font.BOLD, 20)); final JTextArea marketNote = new JTextArea(getMarketingNote(numOfSelected[0])); marketNote.setFont(new Font("Arial", Font.PLAIN, 14)); JScrollPane scrollText = new JScrollPane(marketNote); // buttons for the restocker note interface final JButton save = new JButton("Save Note"); final JButton back = new JButton("Back"); final JLabel info = new JLabel("No changes made"); info.setVisible(true); save.setEnabled(false); // panels for restocker note JPanel buttonFlow = new JPanel(new FlowLayout()); JPanel reNoteFlow = new JPanel(new BorderLayout(0, 20)); // adds to the flow layouts for centering buttonFlow.add(save); buttonFlow.add(info); buttonFlow.add(back); reNoteFlow.add(noteLabel, BorderLayout.NORTH); reNoteFlow.add(scrollText, BorderLayout.CENTER); // adds to the main layout marketPanel.add(topGrid, BorderLayout.NORTH); marketPanel.add(reNoteFlow, BorderLayout.CENTER); marketPanel.add(buttonFlow, BorderLayout.SOUTH); marketNote.addCaretListener( new CaretListener() { @Override public void caretUpdate(CaretEvent e) { String databaseText = getDatabaseText(marketNote.getText()); // System.out.println(databaseText); if (databaseText.equals(getMarketingNote(numOfSelected[0]))) { save.setEnabled(false); info.setText("No changes made"); } else { save.setEnabled(true); info.setText("You need to save your changes"); } } }); back.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub marketPanel.removeAll(); localVendMarketingPanel(marketPanel); marketPanel.revalidate(); marketPanel.repaint(); } }); save.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { try { changeMarketingNote(marketNote.getText()); noteDate.setText(getNoteDate(2, numOfSelected[0])); marketNote.setText(getMarketingNote(numOfSelected[0])); info.setText("Changes Saved"); } catch (NegativeInputException | NullDateException e) { System.err.println("Error. Changing Marketing Note Failed."); // TODO Auto-generated catch block e.printStackTrace(); } } }); return marketPanel; }
/** Initialize the contents of the frame. */ private void initialize() { frmVerbumProcessus = new JFrame(); frmVerbumProcessus.setIconImage( Toolkit.getDefaultToolkit().getImage(window.class.getResource("/resources/rjblogo.png"))); frmVerbumProcessus.setTitle("Verbum Processus"); frmVerbumProcessus.setBounds(100, 100, 613, 460); frmVerbumProcessus.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); CaretListenerLabel caretListenerLabel = new CaretListenerLabel(); final JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); frmVerbumProcessus.getContentPane().add(scrollPane, BorderLayout.CENTER); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); final JPanel panel = new JPanel(); frmVerbumProcessus.getContentPane().add(panel, BorderLayout.SOUTH); String buffer = " "; final JLabel lblStatus = new JLabel("Line: 1, Col: 1"); lblStatus.setHorizontalAlignment(SwingConstants.TRAILING); panel.add(lblStatus); panel.setVisible(false); Dimension d = lblStatus.getPreferredSize(); lblStatus.setPreferredSize(new Dimension(d.width + 120, d.height)); textArea.addCaretListener( new CaretListener() { public void caretUpdate(CaretEvent e) { int caretpos = textArea.getCaretPosition(); int row; int column; try { row = (textArea.getLineOfOffset(caretpos)) + 1; column = caretpos - textArea.getLineStartOffset(row - 1) + 1; lblStatus.setText("Line: " + row + ", Col: " + column); } catch (BadLocationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); JMenuBar menuBar = new JMenuBar(); frmVerbumProcessus.setJMenuBar(menuBar); JMenu mnFile = new JMenu("File"); menuBar.add(mnFile); JMenuItem mntmNew = new JMenuItem("New"); mntmNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK)); mntmNew.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // ###Prompt to save current text Functions.clearCurrentFile(); textArea.setText(null); } }); mnFile.add(mntmNew); JMenuItem mntmExit = new JMenuItem("Exit"); mntmExit.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { System.exit(0); } }); JMenuItem mntmOpen = new JMenuItem("Open"); mntmOpen.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { BufferedReader tempReader = Functions.getTextFromFile(); if (tempReader != null) { textArea.read(tempReader, textArea); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); mnFile.add(mntmOpen); JMenuItem mntmSave = new JMenuItem("Save"); mntmSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); mntmSave.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Functions.saveFile(textArea); } }); mntmSave.setMnemonic(KeyEvent.VK_N); mnFile.add(mntmSave); JMenuItem mntmSaveAs = new JMenuItem("Save As"); mntmSaveAs.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Functions.saveFileAs(textArea); } }); mnFile.add(mntmSaveAs); mnFile.add(mntmExit); JMenu mnEdit = new JMenu("Edit"); menuBar.add(mnEdit); JMenuItem mntmCopy = new JMenuItem("Copy"); mntmCopy.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { textArea.copy(); // Functions.storeText(textArea.getSelectedText()); } }); mntmCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); mnEdit.add(mntmCopy); JMenuItem mntmPaste = new JMenuItem("Paste"); mntmPaste.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { textArea.paste(); } }); mntmPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK)); mnEdit.add(mntmPaste); JMenu mnView = new JMenu("View"); menuBar.add(mnView); JMenuItem mntmStatusBar = new JMenuItem("Status Bar"); mntmStatusBar.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { panel.setVisible(!panel.isVisible()); } }); mnView.add(mntmStatusBar); JMenu mnNewMenu = new JMenu("Help"); menuBar.add(mnNewMenu); JMenuItem mntmAbout = new JMenuItem("About"); mnNewMenu.add(mntmAbout); mntmAbout.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { about info = new about(); info.setLocationRelativeTo(null); info.setVisible(true); } }); mntmAbout.addContainerListener( new ContainerAdapter() { @Override public void componentAdded(ContainerEvent arg0) {} }); }