use of java.awt.Toolkit in project IBMiProgTool by vzupka.
the class EditFile method createWindow.
// End of constructor
/**
* Create window method.
*/
protected void createWindow() {
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
screenWidth = screenSize.width;
screenHeight = screenSize.height;
windowWidth = 850;
windowHeight = screenHeight;
windowX = screenWidth / 2 - windowWidth / 2;
windowY = 0;
menuBar = new JMenuBar();
helpMenu = new JMenu("Help");
helpMenuItemEN = new JMenuItem("Help English");
helpMenuItemCZ = new JMenuItem("Nápověda česky");
helpMenuItemRPGIII = new JMenuItem("RPG III forms");
helpMenuItemRPGIV = new JMenuItem("RPG IV forms");
helpMenuItemCOBOL = new JMenuItem("COBOL form");
helpMenuItemDDS = new JMenuItem("DDS forms");
helpMenu.add(helpMenuItemEN);
helpMenu.add(helpMenuItemCZ);
helpMenu.add(helpMenuItemRPGIII);
helpMenu.add(helpMenuItemRPGIV);
helpMenu.add(helpMenuItemCOBOL);
helpMenu.add(helpMenuItemDDS);
menuBar.add(helpMenu);
editMenu = new JMenu("Edit");
menuUndo = new JMenuItem("Undo");
menuUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
menuRedo = new JMenuItem("Redo");
menuRedo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
menuCut = new JMenuItem("Cut");
menuCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
menuCopy = new JMenuItem("Copy");
menuCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
menuPaste = new JMenuItem("Paste");
menuPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
menuDelete = new JMenuItem("Delete");
menuDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
menuDelete2 = new JMenuItem("Delete");
menuDelete2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0));
menuFind = new JMenuItem("Find");
menuFind.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
menuBar.add(editMenu);
editMenu.add(menuUndo);
editMenu.add(menuRedo);
editMenu.addSeparator();
editMenu.add(menuCut);
editMenu.add(menuCopy);
editMenu.add(menuPaste);
editMenu.addSeparator();
editMenu.add(menuDelete);
editMenu.add(menuDelete2);
editMenu.addSeparator();
editMenu.add(menuFind);
// In macOS on the main system menu bar above, in Windows on the window menu bar
setJMenuBar(menuBar);
originalButtonBackground = new JButton().getBackground();
originalButtonForeground = new JButton().getForeground();
saveButton.setPreferredSize(new Dimension(80, 20));
saveButton.setMinimumSize(new Dimension(80, 20));
saveButton.setMaximumSize(new Dimension(80, 20));
saveButton.setToolTipText("Also Ctrl+S (Cmd+S in macOS).");
saveButton.setFont(saveButton.getFont().deriveFont(Font.BOLD, 12));
// Save button will have the original black color.
textChanged = false;
undoButton.setPreferredSize(new Dimension(60, 20));
undoButton.setMinimumSize(new Dimension(60, 20));
undoButton.setMaximumSize(new Dimension(60, 20));
redoButton.setPreferredSize(new Dimension(60, 20));
redoButton.setMinimumSize(new Dimension(60, 20));
redoButton.setMaximumSize(new Dimension(60, 20));
caretButton.setPreferredSize(new Dimension(90, 20));
caretButton.setMinimumSize(new Dimension(90, 20));
caretButton.setMaximumSize(new Dimension(90, 20));
caretButton.setToolTipText("Toggle short or long caret. Also right click in text area.");
selectionModeButton.setPreferredSize(new Dimension(150, 20));
selectionModeButton.setMinimumSize(new Dimension(150, 20));
selectionModeButton.setMaximumSize(new Dimension(150, 20));
selectionModeButton.setToolTipText("Toggle horizontal or vertical selection. Also right click in text area.");
// Set selection mode as the button text
selectionModeButton.setText(selectionMode);
compileButton.setPreferredSize(new Dimension(80, 20));
compileButton.setMinimumSize(new Dimension(80, 20));
compileButton.setMaximumSize(new Dimension(80, 20));
compileButton.setFont(compileButton.getFont().deriveFont(Font.BOLD, 12));
compileButton.setToolTipText("Open window with compile settings.");
fontComboBox.setPreferredSize(new Dimension(140, 20));
fontComboBox.setMaximumSize(new Dimension(140, 20));
fontComboBox.setMinimumSize(new Dimension(140, 20));
fontComboBox.setToolTipText("Choose font.");
// Sets the current editor font item into the input field of the combo box
fontComboBox.setSelectedItem(editorFont);
// This class gives the corresponding fonts to the font names in the combo box list
fontComboBox.setRenderer(new FontComboBoxRenderer());
fontSizeField.setText(fontSizeString);
fontSizeField.setPreferredSize(new Dimension(30, 20));
fontSizeField.setMaximumSize(new Dimension(30, 20));
fontSizeField.setToolTipText("Enter font size.");
characterSetLabel.setForeground(DIM_BLUE);
highlightBlocksLabel.setToolTipText("Blocks are compound statements like IF - ENDIF, etc.");
languageComboBox.setPreferredSize(new Dimension(130, 20));
languageComboBox.setMaximumSize(new Dimension(130, 20));
languageComboBox.setMinimumSize(new Dimension(130, 20));
languageComboBox.setToolTipText("Highlight blocks. Choose programming language. Blocks are compound statements like IF - ENDIF, etc.");
languageComboBox.addItem("*NONE");
languageComboBox.addItem("*ALL");
languageComboBox.addItem("RPG **FREE");
languageComboBox.addItem("RPG /FREE");
languageComboBox.addItem("RPG IV fixed");
languageComboBox.addItem("RPG III");
languageComboBox.addItem("COBOL");
languageComboBox.addItem("CL");
languageComboBox.addItem("C");
languageComboBox.addItem("C++");
languageComboBox.setSelectedItem(progLanguage);
// Shift left icon and button
ImageIcon shiftLeftImageIcon = new ImageIcon(shiftLeftIconPath.toString());
leftShiftButton = new JButton(shiftLeftImageIcon);
leftShiftButton.setToolTipText("Shift selection left. Also Ctrl+⬅ (Cmd+⬅ in macOS).");
leftShiftButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
leftShiftButton.setContentAreaFilled(false);
leftShiftButton.setPreferredSize(new Dimension(20, 20));
leftShiftButton.setMinimumSize(new Dimension(20, 20));
leftShiftButton.setMaximumSize(new Dimension(20, 20));
ImageIcon shiftRightImageIcon = new ImageIcon(shiftRightIconPath.toString());
rightShiftButton = new JButton(shiftRightImageIcon);
rightShiftButton.setToolTipText("Shift selection right. Also Ctrl+➜(Cmd+➜ in macOS).");
rightShiftButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
rightShiftButton.setContentAreaFilled(false);
rightShiftButton.setPreferredSize(new Dimension(20, 20));
rightShiftButton.setMinimumSize(new Dimension(20, 20));
rightShiftButton.setMaximumSize(new Dimension(20, 20));
// Magnifying glass icon and button
ImageIcon findImageIcon = new ImageIcon(findIconPath.toString());
findButton = new JButton(findImageIcon);
findButton.setToolTipText("Find and replace text. Also Ctrl+F (Cmd+F in macOS).");
findButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
findButton.setContentAreaFilled(false);
findButton.setPreferredSize(new Dimension(20, 20));
findButton.setMinimumSize(new Dimension(20, 20));
findButton.setMaximumSize(new Dimension(20, 20));
// Split icon and button
ImageIcon splitImageIcon = new ImageIcon(splitIconPath.toString());
splitUnsplitButton = new JButton(splitImageIcon);
splitUnsplitButton.setToolTipText("Split/unsplit editor area.");
splitUnsplitButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
splitUnsplitButton.setContentAreaFilled(false);
splitUnsplitButton.setPreferredSize(new Dimension(20, 20));
splitUnsplitButton.setMinimumSize(new Dimension(20, 20));
splitUnsplitButton.setMaximumSize(new Dimension(20, 20));
// Undo icon and button
ImageIcon undoImageIcon = new ImageIcon(undoIconPath.toString());
undoButton = new JButton(undoImageIcon);
undoButton.setToolTipText("Undo. Also Ctrl+Z (Cmd+Z in macOS).");
undoButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
undoButton.setContentAreaFilled(false);
undoButton.setPreferredSize(new Dimension(20, 20));
undoButton.setMinimumSize(new Dimension(20, 20));
undoButton.setMaximumSize(new Dimension(20, 20));
// Redo icon and button
ImageIcon redoImageIcon = new ImageIcon(redoIconPath.toString());
redoButton = new JButton(redoImageIcon);
redoButton.setToolTipText("Redo. Also Ctrl+Y (Cmd+Y in macOS).");
redoButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
redoButton.setContentAreaFilled(false);
redoButton.setPreferredSize(new Dimension(20, 20));
redoButton.setMinimumSize(new Dimension(20, 20));
redoButton.setMaximumSize(new Dimension(20, 20));
// Split pane (divided by horizontal line) containing two scroll panes
splitVerticalPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
// Build editor area
// =================
textArea.setEditable(true);
textArea.setFont(new Font(editorFont, Font.PLAIN, fontSize));
textArea.setTabSize(TAB_SIZE);
textArea2.setFont(new Font(editorFont, Font.PLAIN, fontSize));
textArea2.setTabSize(TAB_SIZE);
textArea.setDragEnabled(true);
// Create a scroll pane
scrollPane = new JScrollPane(textArea);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// Light sky blue for IBM i (PC color is resolved in the constructor later).
scrollPane.setBackground(VERY_LIGHT_BLUE);
textArea.setBackground(VERY_LIGHT_BLUE);
// Now the scroll pane may be sized because window height is defined
scrollPane.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
scrollPane.setPreferredSize(new Dimension(windowWidth, windowHeight));
// Custom deletion will be active in VERTICAL selection mode only.
if (selectionMode.equals(VERTICAL_SELECTION)) {
// Activate custom deletion by Delete or Backspace key
textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "deleteDel");
textArea.getActionMap().put("deleteDel", new CustomDelete("DEL"));
textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), "deleteBcksp");
textArea.getActionMap().put("deleteBcksp", new CustomDelete("BACKSPACE"));
textArea2.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "deleteDel");
textArea2.getActionMap().put("deleteDel", new CustomDelete("DEL"));
textArea2.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), "deleteBcksp");
textArea2.getActionMap().put("deleteBcksp", new CustomDelete("BACKSPACE"));
} else {
// Deactivate custom deletion in horizontal mode
textArea.getInputMap(JComponent.WHEN_FOCUSED).remove(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
textArea.getInputMap(JComponent.WHEN_FOCUSED).remove(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0));
textArea.getActionMap().remove("deleteDel");
textArea.getActionMap().remove("deleteBcksp");
textArea2.getInputMap(JComponent.WHEN_FOCUSED).remove(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
textArea2.getInputMap(JComponent.WHEN_FOCUSED).remove(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0));
textArea2.getActionMap().remove("deleteDel");
textArea2.getActionMap().remove("deleteBcksp");
}
rowPanel1 = new JPanel();
rowPanel1.setLayout(new BoxLayout(rowPanel1, BoxLayout.X_AXIS));
rowPanel1.add(caretButton);
rowPanel1.add(Box.createHorizontalStrut(10));
rowPanel1.add(selectionModeButton);
rowPanel1.add(Box.createHorizontalStrut(10));
// rowPanel1.add(shiftLabel);
rowPanel1.add(leftShiftButton);
rowPanel1.add(Box.createHorizontalStrut(10));
rowPanel1.add(rightShiftButton);
rowPanel1.add(Box.createHorizontalStrut(20));
rowPanel1.add(undoButton);
rowPanel1.add(Box.createHorizontalStrut(10));
rowPanel1.add(redoButton);
rowPanel1.add(Box.createHorizontalStrut(20));
rowPanel1.add(saveButton);
// Compile button is not available in PC.
if (!methodName.equals("rewritePcFile")) {
rowPanel1.add(Box.createHorizontalStrut(20));
rowPanel1.add(compileButton);
}
saveButton.setSelected(true);
rowPanel2 = new JPanel();
GroupLayout rowPanel2Layout = new GroupLayout(rowPanel2);
rowPanel2Layout.setHorizontalGroup(rowPanel2Layout.createSequentialGroup().addGap(0).addComponent(splitUnsplitButton).addGap(20).addComponent(fontComboBox).addComponent(fontSizeField).addGap(20).addComponent(languageComboBox).addGap(40).addComponent(findButton).addGap(60).addComponent(characterSetLabel));
rowPanel2Layout.setVerticalGroup(rowPanel2Layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(splitUnsplitButton).addComponent(fontComboBox).addComponent(fontSizeField).addComponent(languageComboBox).addComponent(findButton).addComponent(characterSetLabel));
rowPanel2.setLayout(rowPanel2Layout);
globalPanel = new JPanel();
GroupLayout topPanelLayout = new GroupLayout(globalPanel);
topPanelLayout.setHorizontalGroup(topPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(rowPanel2).addComponent(rowPanel1).addComponent(scrollPane));
topPanelLayout.setVerticalGroup(topPanelLayout.createSequentialGroup().addComponent(rowPanel2).addGap(2).addComponent(rowPanel1).addGap(4).addComponent(scrollPane));
globalPanel.setLayout(topPanelLayout);
globalPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
add(globalPanel);
// Display the window.
setSize(windowWidth, windowHeight);
setLocation(windowX, windowY);
// pack();
setVisible(true);
// Register listeners
// ==================
TextAreaInitDocListener textAreaInitDocListener = new TextAreaInitDocListener();
textArea.getDocument().addDocumentListener(textAreaInitDocListener);
// Listener for undoable edits
textArea.getDocument().addUndoableEditListener(undoHandler);
// Register HelpWindow menu item listener
helpMenuItemEN.addActionListener(ae -> {
String command = ae.getActionCommand();
if (command.equals("Help English")) {
if (Desktop.isDesktopSupported()) {
String uri = Paths.get(System.getProperty("user.dir"), "helpfiles", "IBMiProgTool_doc_EN.pdf").toString();
// Replace backslashes by forward slashes in Windows
uri = uri.replace('\\', '/');
uri = uri.replace(" ", "%20");
try {
// Invoke the standard browser in the operating system
Desktop.getDesktop().browse(new URI("file://" + uri));
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
});
// Register HelpWindow menu item listener
helpMenuItemCZ.addActionListener(ae -> {
String command = ae.getActionCommand();
if (command.equals("Nápověda česky")) {
if (Desktop.isDesktopSupported()) {
String uri = Paths.get(System.getProperty("user.dir"), "helpfiles", "IBMiProgTool_doc_CZ.pdf").toString();
// Replace backslashes by forward slashes in Windows
uri = uri.replace('\\', '/');
uri = uri.replace(" ", "%20");
try {
// Invoke the standard browser in the operating system
Desktop.getDesktop().browse(new URI("file://" + uri));
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
});
// Register HelpWindow menu item listener
helpMenuItemRPGIII.addActionListener(ae -> {
String command = ae.getActionCommand();
if (command.equals("RPG III forms")) {
if (Desktop.isDesktopSupported()) {
String uri = Paths.get(System.getProperty("user.dir"), "helpfiles", "RPG_III_forms.pdf").toString();
// Replace backslashes by forward slashes in Windows
uri = uri.replace('\\', '/');
try {
// Invoke the standard browser in the operating system
Desktop.getDesktop().browse(new URI("file://" + uri));
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
});
// Register HelpWindow menu item listener
helpMenuItemRPGIV.addActionListener(ae -> {
String command = ae.getActionCommand();
if (command.equals("RPG IV forms")) {
if (Desktop.isDesktopSupported()) {
String uri = Paths.get(System.getProperty("user.dir"), "helpfiles", "RPG_IV_forms.pdf").toString();
// Replace backslashes by forward slashes in Windows
uri = uri.replace('\\', '/');
uri = uri.replace(" ", "%20");
try {
// Invoke the standard browser in the operating system
Desktop.getDesktop().browse(new URI("file://" + uri));
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
});
// Register HelpWindow menu item listener
helpMenuItemCOBOL.addActionListener(ae -> {
String command = ae.getActionCommand();
if (command.equals("COBOL form")) {
if (Desktop.isDesktopSupported()) {
String uri = Paths.get(System.getProperty("user.dir"), "helpfiles", "COBOL_form.pdf").toString();
// Replace backslashes by forward slashes in Windows
uri = uri.replace('\\', '/');
uri = uri.replace(" ", "%20");
try {
// Invoke the standard browser in the operating system
Desktop.getDesktop().browse(new URI("file://" + uri));
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
});
// Register HelpWindow menu item listener
helpMenuItemDDS.addActionListener(ae -> {
String command = ae.getActionCommand();
if (command.equals("DDS forms")) {
if (Desktop.isDesktopSupported()) {
String uri = Paths.get(System.getProperty("user.dir"), "helpfiles", "DDS_forms.pdf").toString();
// Replace backslashes by forward slashes in Windows
uri = uri.replace('\\', '/');
uri = uri.replace(" ", "%20");
try {
// Invoke the standard browser in the operating system
Desktop.getDesktop().browse(new URI("file://" + uri));
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
});
// Select editor font from the list in combo box - listener
// --------------------------------------------------------
fontComboBox.addItemListener(il -> {
int currentCaretPos = textArea.getCaretPosition();
JComboBox<String> source = (JComboBox) il.getSource();
fontSizeString = fontSizeField.getText();
try {
fontSize = Integer.parseInt(fontSizeString);
} catch (Exception exc) {
exc.printStackTrace();
fontSizeString = "12";
fontSize = 12;
}
editorFont = (String) fontComboBox.getSelectedItem();
textArea.setFont(new Font(editorFont, Font.PLAIN, fontSize));
textArea2.setFont(new Font(editorFont, Font.PLAIN, fontSize));
try {
BufferedWriter outfile = Files.newBufferedWriter(parPath, Charset.forName(encoding));
// Save programming language into properties
properties.setProperty("EDITOR_FONT", editorFont);
properties.setProperty("EDITOR_FONT_SIZE", fontSizeString);
properties.store(outfile, PROP_COMMENT);
outfile.close();
} catch (Exception exc) {
exc.printStackTrace();
}
// Prepare text area with highlighting blocks and show
prepareEditingAndShow();
textArea.requestFocusInWindow();
textArea.setCaretPosition(currentCaretPos);
});
// "Font size" field listener
// --------------------------
fontSizeField.addActionListener(al -> {
int currentCaretPos = textArea.getCaretPosition();
fontSizeString = fontSizeField.getText();
try {
fontSize = Integer.parseInt(fontSizeString);
} catch (Exception exc) {
exc.printStackTrace();
fontSizeString = "12";
fontSize = 12;
}
fontSizeField.setText(fontSizeString);
textArea.setFont(new Font(editorFont, Font.PLAIN, fontSize));
textArea2.setFont(new Font(editorFont, Font.PLAIN, fontSize));
try {
BufferedWriter outfile = Files.newBufferedWriter(parPath, Charset.forName(encoding));
// Save font size into properties
properties.setProperty("EDITOR_FONT", editorFont);
properties.setProperty("EDITOR_FONT_SIZE", fontSizeString);
properties.store(outfile, PROP_COMMENT);
outfile.close();
} catch (Exception exc) {
exc.printStackTrace();
}
// Prepare text area with highlighting blocks and show
// prepareEditingAndShow();
textArea.requestFocusInWindow();
textArea.setCaretPosition(currentCaretPos);
});
// Select programming language from the list in combo box - listener
// -----------------------------------------------------------------
languageComboBox.addItemListener(il -> {
// Remember caret position
int currentCaretPos = textArea.getCaretPosition();
JComboBox<String> source = (JComboBox) il.getSource();
progLanguage = (String) source.getSelectedItem();
// Highlight possible matched patterns in both primary and secondary areas
changeHighlight();
changeHighlight2();
try {
BufferedWriter outfile = Files.newBufferedWriter(parPath, Charset.forName(encoding));
// Save programming language into properties
properties.setProperty("HIGHLIGHT_BLOCKS", progLanguage);
properties.store(outfile, PROP_COMMENT);
outfile.close();
} catch (Exception exc) {
exc.printStackTrace();
}
// Prepare both text areas:
// - Set caret position in primary text area.
// - Highlight blocks.
// - Update progLanguage property in parameters.
prepareEditingAndShow();
textArea.requestFocusInWindow();
// Set remembered caret position
textArea.setCaretPosition(currentCaretPos);
});
// Caret button and popup menu listeners
// =====================================
toggleCaret.addActionListener(ae -> {
changeCaretShape();
});
caretButton.addActionListener(ae -> {
changeCaretShape();
});
// Selection mode listeners
// ========================
selectionModeButton.addActionListener(ae -> {
changeSelectionMode();
});
changeSelMode.addActionListener(ae -> {
changeSelectionMode();
});
// Find button listener
// --------------------
CreateFindWindow createFindWindow = new CreateFindWindow();
findButton.addActionListener(createFindWindow);
// Split/Unsplit button listener
// -----------------------------
splitUnsplitButton.addActionListener(ae -> {
if (!textAreaIsSplit) {
caretPosition = textArea.getCaretPosition();
splitTextArea();
textAreaIsSplit = true;
} else if (textAreaIsSplit) {
unsplitTextArea();
textAreaIsSplit = false;
}
});
// Undo button listener and menu item listener
undoAction = new UndoAction();
undoButton.addActionListener(undoAction);
menuUndo.addActionListener(undoAction);
// Redo button listener and menu item listener
redoAction = new RedoAction();
redoButton.addActionListener(redoAction);
menuRedo.addActionListener(redoAction);
// Cut menu item listener
CustomCut customCut = new CustomCut();
menuCut.addActionListener(customCut);
// Copy menu item listener
CustomCopy customCopy = new CustomCopy();
menuCopy.addActionListener(customCopy);
// Paste menu item listener
CustomPaste customPaste = new CustomPaste();
menuPaste.addActionListener(customPaste);
// Delete DEL menu item listener
CustomDelete customDelete = new CustomDelete("DEL");
menuDelete.addActionListener(customDelete);
// Delet BACKSPACE menu item listener
CustomDelete customDelete2 = new CustomDelete("BACKSPACE");
menuDelete2.addActionListener(customDelete2);
// Find menu item listener
CreateFindWindow findWindow = new CreateFindWindow();
menuFind.addActionListener(findWindow);
// Save button listener
// --------------------
SaveAction saveAction = new SaveAction();
saveButton.setToolTipText("Also Ctrl+S (Cmd+S in macOS).");
saveButton.addActionListener(saveAction);
// Left shift button listener
// --------------------------
ArrowLeft arrowLeft = new ArrowLeft();
leftShiftButton.addActionListener(arrowLeft);
// Right shift button listener
// ---------------------------
ArrowRight arrowRight = new ArrowRight();
rightShiftButton.addActionListener(arrowRight);
// Compile button listener
// -----------------------
compileButtonListener = new CompileButtonListener();
compileButton.addActionListener(compileButtonListener);
// Keyboard key listeners
// ----------------------
// Enable ESCAPE key to escape from editing
globalPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "escapeCmd");
globalPanel.getActionMap().put("escapeCmd", new Escape());
// Enable processing of function key Ctrl + S = Save data
globalPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "save");
globalPanel.getActionMap().put("save", saveAction);
// Enable processing of function key Ctrl + Z = Undo
globalPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "undo");
globalPanel.getActionMap().put("undo", undoAction);
textArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "undo");
textArea.getActionMap().put("undo", undoAction);
textArea2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "undo");
textArea2.getActionMap().put("undo", undoAction);
// Enable processing of function key Ctrl + Y = Redo
globalPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "redo");
globalPanel.getActionMap().put("redo", redoAction);
textArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "redo");
textArea.getActionMap().put("redo", redoAction);
textArea2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "redo");
textArea2.getActionMap().put("redo", redoAction);
// Enable processing of function key Ctrl + F = create FindWidnow
globalPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "crtFindWindow");
globalPanel.getActionMap().put("crtFindWindow", createFindWindow);
// Enable processing of function key Ctrl + F = create FindWidnow
textArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "crtFindWindow");
textArea.getActionMap().put("crtFindWindow", createFindWindow);
// Enable processing of function key Ctrl + F = create FindWidnow
textArea2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "crtFindWindow");
textArea2.getActionMap().put("crtFindWindow", createFindWindow);
// Enable processing of Tab key
globalPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("TAB"), "tab");
globalPanel.getActionMap().put("tab", new TabListener());
// Enable processing of function key Ctrl + Arrow Left = Shift lines or rectangle left
textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "shiftLeft");
textArea.getActionMap().put("shiftLeft", arrowLeft);
// Enable processing of function key Ctrl + Arrow Right = Shift lines or rectangle right
textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "shiftRight");
textArea.getActionMap().put("shiftRight", arrowRight);
// Enable processing of function key Ctrl + Arrow Left = Shift lines or rectangle left
textArea2.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "shiftLeft");
textArea2.getActionMap().put("shiftLeft", arrowLeft);
// Enable processing of function key Ctrl + Arrow Right = Shift lines or rectangle right
textArea2.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "shiftRight");
textArea2.getActionMap().put("shiftRight", arrowRight);
// Enable custom processing of function key Ctrl C = Custom copy
textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), "copy");
textArea.getActionMap().put("copy", new CustomCopy());
// Enable custom processing of function key Ctrl X = Custom cut
textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK), "cut");
textArea.getActionMap().put("cut", new CustomCut());
// Enable custom processing of function key Ctrl V = Custom paste
textArea.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK), "paste");
textArea.getActionMap().put("paste", new CustomPaste());
// Enable custom processing of function key Ctrl C = Custom copy
textArea2.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), "copy");
textArea2.getActionMap().put("copy", new CustomCopy());
// Enable custom processing of function key Ctrl X = Custom cut
textArea2.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK), "cut");
textArea2.getActionMap().put("cut", new CustomCut());
// Enable custom processing of function key Ctrl V = Custom paste
textArea2.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK), "paste");
textArea2.getActionMap().put("paste", new CustomPaste());
// Mouse listeners for text areas
// ------------------------------
textAreaMouseListener = new TextAreaMouseListener();
textArea.addMouseListener(textAreaMouseListener);
textArea2MouseListener = new TextArea2MouseListener();
textArea2.addMouseListener(textArea2MouseListener);
// Window listener
// ---------------
windowEditFileListener = new WindowEditFileAdapter();
this.addWindowListener(windowEditFileListener);
{
// IMPORTANT: This block must be run AFTER MOUSE LISTENER REGISTRATION
// so that double click select a word with the BASIC caret.
// Choose initial caret shape
// --------------------------
// Special caret for vertical (rectangular) selection mode
specialCaret = new SpecialCaret();
specialCaret2 = new SpecialCaret2();
// Long caret for horizontal selection mode
longCaret = new LongCaret();
longCaret2 = new LongCaret2();
// Short catet for primary text area
basicCaret = new BasicTextUI.BasicCaret();
// Short caret for secondary text area
basicCaret2 = new BasicTextUI.BasicCaret();
// Caret button with short or long caret
// Caret shape from parameters
caretButton.setText(caretShape);
// The secondary textArea2 will be assigned the caret in the "splitUnsplitButton" listener.
if (caretShape.equals(LONG_CARET)) {
if (selectionMode.equals(HORIZONTAL_SELECTION)) {
// Horizontal selection
// Set custom caret - long vertical gray line with a short red pointer
textArea.setCaret(longCaret);
} else {
// Vertical selection
// Set custom caret - long vertical gray line with a short red pointer
textArea.setCaret(specialCaret);
}
} else {
if (selectionMode.equals(HORIZONTAL_SELECTION)) {
// Horizontal selection
// For short caret set basic caret - a short vertical line
textArea.setCaret(basicCaret);
} else {
// Vertical selection
// Set custom caret - long vertical gray line with a short red pointer
textArea.setCaret(specialCaret);
}
}
}
// end of block
}
use of java.awt.Toolkit in project CodenameOne by codenameone.
the class JavaSEPort method init.
/**
* @inheritDoc
*/
public void init(Object m) {
inInit = true;
File updater = new File(System.getProperty("user.home") + File.separator + ".codenameone" + File.separator + "UpdateCodenameOne.jar");
if (!updater.exists()) {
System.out.println("******************************************************************************");
System.out.println("* It seems that you are using an old plugin version please upate to the latest plugin and invoke Codename One -> Codename One Settings -> Basic -> Update Client Libs");
System.out.println("******************************************************************************");
}
Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
boolean desktopSkin = pref.getBoolean("desktopSkin", false);
if (desktopSkin && m == null) {
Toolkit tk = Toolkit.getDefaultToolkit();
setDefaultPixelMilliRatio(tk.getScreenResolution() / 25.4 * getRetinaScale());
pixelMilliRatio = getDefaultPixelMilliRatio();
JPanel panel = new javax.swing.JPanel();
panel.setLayout(new BorderLayout());
JPanel bottom = new javax.swing.JPanel();
panel.setOpaque(false);
bottom.setLayout(new FlowLayout(FlowLayout.RIGHT));
widthLabel = new JLabel("Width: ");
heightLabel = new JLabel(" Height: ");
bottom.add(widthLabel);
bottom.add(heightLabel);
panel.add(bottom, BorderLayout.SOUTH);
JFrame frame = new JFrame();
// frame.addWindowListener(new WindowListener() {
//
// });
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setSize(new Dimension(300, 400));
m = panel;
window = frame;
}
setInvokePointerHover(desktopSkin || invokePointerHover);
// this is essential for push and other things to work in the simulator
Preferences p = Preferences.userNodeForPackage(com.codename1.ui.Component.class);
String user = p.get("user", null);
if (user != null) {
Display d = Display.getInstance();
d.setProperty("built_by_user", user);
String mainClass = System.getProperty("MainClass");
if (mainClass != null) {
mainClass = mainClass.substring(0, mainClass.lastIndexOf('.'));
d.setProperty("package_name", mainClass);
}
}
try {
Class.forName("javafx.embed.swing.JFXPanel");
Platform.setImplicitExit(false);
fxExists = true;
} catch (Throwable ex) {
}
String hide = System.getProperty("hideMenu", "false");
if (hide != null && hide.equals("true")) {
showMenu = false;
}
URLConnection.setDefaultAllowUserInteraction(true);
HttpURLConnection.setFollowRedirects(false);
if (!blockMonitors && pref.getBoolean("NetworkMonitor", false)) {
showNetworkMonitor();
}
if (!blockMonitors && pref.getBoolean("PushSimulator", false)) {
pushSimulation = new PushSimulator();
pushSimulation.setVisible(true);
}
if (!blockMonitors && pref.getBoolean("PerformanceMonitor", false)) {
showPerformanceMonitor();
}
if (defaultInitTarget != null && m == null) {
m = defaultInitTarget;
}
if (canvas.getParent() != null) {
canvas.getParent().remove(canvas);
}
if (m != null && m instanceof java.awt.Container) {
java.awt.Container cnt = (java.awt.Container) m;
if (cnt.getLayout() instanceof java.awt.BorderLayout) {
cnt.add(java.awt.BorderLayout.CENTER, canvas);
} else {
cnt.add(canvas);
}
} else {
window = new JFrame();
window.setLayout(new java.awt.BorderLayout());
hSelector = new JScrollBar(Scrollbar.HORIZONTAL);
vSelector = new JScrollBar(Scrollbar.VERTICAL);
hSelector.addAdjustmentListener(canvas);
vSelector.addAdjustmentListener(canvas);
scrollableSkin = pref.getBoolean("Scrollable", true);
if (scrollableSkin) {
window.add(java.awt.BorderLayout.SOUTH, hSelector);
window.add(java.awt.BorderLayout.EAST, vSelector);
}
window.add(java.awt.BorderLayout.CENTER, canvas);
}
if (window != null) {
java.awt.Image large = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/application64.png"));
java.awt.Image small = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/application48.png"));
try {
// setIconImages is only available in JDK 1.6
window.setIconImages(Arrays.asList(new java.awt.Image[] { large, small }));
} catch (Throwable err) {
window.setIconImage(small);
}
window.addWindowListener(new WindowListener() {
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
Display.getInstance().exitApplication();
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
});
window.setLocationByPlatform(true);
android6PermissionsFlag = pref.getBoolean("Android6Permissions", false);
alwaysOnTop = pref.getBoolean("AlwaysOnTop", false);
window.setAlwaysOnTop(alwaysOnTop);
String reset = System.getProperty("resetSkins");
if (reset != null && reset.equals("true")) {
System.setProperty("resetSkins", "");
pref = Preferences.userNodeForPackage(JavaSEPort.class);
pref.put("skins", DEFAULT_SKINS);
}
if (hasSkins()) {
if (m == null) {
String f = System.getProperty("skin");
if (f != null) {
loadSkinFile(f, window);
} else {
String d = System.getProperty("dskin");
f = pref.get("skin", d);
loadSkinFile(f, window);
}
} else {
try {
installMenu(window, true);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
Resources.setRuntimeMultiImageEnabled(true);
window.setUndecorated(true);
window.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
window.pack();
if (getSkin() != null && !scrollableSkin) {
float w1 = ((float) canvas.getWidth() * (float) retinaScale) / ((float) getSkin().getWidth());
float h1 = ((float) canvas.getHeight() * (float) retinaScale) / ((float) getSkin().getHeight());
zoomLevel = Math.min(h1, w1);
}
portrait = pref.getBoolean("Portrait", true);
if (!portrait && getSkin() != null) {
canvas.setForcedSize(new java.awt.Dimension((int) (getSkin().getWidth() / retinaScale), (int) (getSkin().getHeight() / retinaScale)));
window.setSize(new java.awt.Dimension((int) (getSkin().getWidth() / retinaScale), (int) (getSkin().getHeight() / retinaScale)));
}
window.setVisible(true);
}
if (useNativeInput) {
Display.getInstance().setDefaultVirtualKeyboard(null);
}
float factor = ((float) getDisplayHeight()) / 480.0f;
if (factor > 0 && autoAdjustFontSize && getSkin() != null) {
// set a reasonable default font size
setFontSize((int) (15.0f * factor), (int) (11.0f * factor), (int) (19.0f * factor));
}
if (m instanceof Runnable) {
Display.getInstance().callSerially((Runnable) m);
}
inInit = false;
}
use of java.awt.Toolkit in project keystore-explorer by kaikramer.
the class KeyStoreEntryDragGestureListener method dragGestureRecognized.
/**
* Drag gesture recognized. Start the drag off if valid.
*
* @param evt
* Drag gesture event
*/
@Override
public void dragGestureRecognized(DragGestureEvent evt) {
DragEntry dragEntry = kseFrame.dragSelectedEntry();
if (dragEntry == null) {
return;
}
ImageIcon icon = dragEntry.getImage();
// Draw image as drag cursor
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dim = toolkit.getBestCursorSize(icon.getIconWidth(), icon.getIconHeight());
BufferedImage buffImage = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB_PRE);
icon.paintIcon(evt.getComponent(), buffImage.getGraphics(), 0, 0);
cursor = toolkit.createCustomCursor(buffImage, new Point(0, 0), "keystore-entry");
evt.startDrag(cursor, new KeyStoreEntryTransferable(dragEntry), this);
}
use of java.awt.Toolkit in project lionengine by b3dgs.
the class ToolsAwt method createHiddenCursor.
/**
* Create a hidden cursor.
*
* @return Hidden cursor, or default cursor if not able to create it.
*/
public static Cursor createHiddenCursor() {
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension dim = toolkit.getBestCursorSize(1, 1);
final BufferedImage c = createImage(Math.max(1, dim.width), Math.max(1, dim.height), java.awt.Transparency.BITMASK);
final BufferedImage buffer = applyMask(c, Color.BLACK.getRGB());
return toolkit.createCustomCursor(buffer, new Point(0, 0), "hiddenCursor");
}
use of java.awt.Toolkit in project vcell by virtualcell.
the class FRAPClipboard method sendToClip.
public void sendToClip() {
// Gets tools from system.
Toolkit tools = Toolkit.getDefaultToolkit();
Clipboard clip = tools.getSystemClipboard();
clip.setContents(this, this);
}
Aggregations