public void paintComponent(Graphics g) { if (name == "" && serialport == "") { Map<String, String> boardPreferences = Base.getBoardPreferences(); if (boardPreferences != null) setBoardName(boardPreferences.get("name")); else setBoardName("-"); setSerialPort(Preferences.get("serial.port")); } g.setColor(background); Dimension size = getSize(); g.fillRect(0, 0, size.width, size.height); g.setFont(font); g.setColor(foreground); int baseline = (high + g.getFontMetrics().getAscent()) / 2; g.drawString(text, 6, baseline); g.setColor(messageForeground); String tmp = name + " on " + serialport; Rectangle2D bounds = g.getFontMetrics().getStringBounds(tmp, null); g.drawString(tmp, size.width - (int) bounds.getWidth() - 20, baseline); if (Base.isMacOS()) { g.drawImage(resize, size.width - 20, 0, this); } }
/** * Make sure the sketch hasn't been moved or deleted by some nefarious user. If they did, try to * re-create it and save. Only checks to see if the main folder is still around, but not its * contents. */ private void ensureExistence() { if (sketch.getFolder().exists()) return; Base.showWarning( tr("Sketch Disappeared"), tr( "The sketch folder has disappeared.\n " + "Will attempt to re-save in the same location,\n" + "but anything besides the code will be lost."), null); try { sketch.getFolder().mkdirs(); for (SketchFile file : sketch.getFiles()) { file.save(); // this will force a save } calcModified(); } catch (Exception e) { Base.showWarning( tr("Could not re-save sketch"), tr( "Could not properly re-save the sketch. " + "You may be in trouble at this point,\n" + "and it might be time to copy and paste " + "your code to another text editor."), e); } }
/** Handler for the Rename Code menu option. */ public void handleRenameCode() { SketchFile current = editor.getCurrentTab().getSketchFile(); editor.status.clearState(); // make sure the user didn't hide the sketch folder ensureExistence(); if (current.isPrimary() && editor.untitled) { Base.showMessage( tr("Sketch is Untitled"), tr("How about saving the sketch first \n" + "before trying to rename it?")); return; } // if read-only, give an error if (isReadOnly( BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) { // if the files are read-only, need to first do a "save as". Base.showMessage( tr("Sketch is Read-Only"), tr( "Some files are marked \"read-only\", so you'll\n" + "need to re-save the sketch in another location,\n" + "and try again.")); return; } // ask for new name of file (internal to window) // TODO maybe just popup a text area? renamingCode = true; String prompt = current.isPrimary() ? "New name for sketch:" : "New name for file:"; String oldName = current.getPrettyName(); editor.status.edit(prompt, oldName); }
/** Remove a piece of code from the sketch and from the disk. */ public void handleDeleteCode() throws IOException { SketchFile current = editor.getCurrentTab().getSketchFile(); editor.status.clearState(); // make sure the user didn't hide the sketch folder ensureExistence(); // if read-only, give an error if (isReadOnly( BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) { // if the files are read-only, need to first do a "save as". Base.showMessage( tr("Sketch is Read-Only"), tr( "Some files are marked \"read-only\", so you'll\n" + "need to re-save the sketch in another location,\n" + "and try again.")); return; } // confirm deletion with user, yes/no Object[] options = {tr("OK"), tr("Cancel")}; String prompt = current.isPrimary() ? tr("Are you sure you want to delete this sketch?") : I18n.format(tr("Are you sure you want to delete \"{0}\"?"), current.getPrettyName()); int result = JOptionPane.showOptionDialog( editor, prompt, tr("Delete"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (result == JOptionPane.YES_OPTION) { if (current.isPrimary()) { sketch.delete(); editor.base.handleClose(editor); } else { // delete the file if (!current.delete(sketch.getBuildPath().toPath())) { Base.showMessage( tr("Couldn't do it"), I18n.format(tr("Could not delete \"{0}\"."), current.getFileName())); return; } // just set current tab to the main tab editor.selectTab(0); // update the tabs editor.header.repaint(); } } }
/** Add import statements to the current tab for all of packages inside the specified jar file. */ public void handleImportLibrary(String jarPath) { // make sure the user didn't hide the sketch folder sketch.ensureExistence(); // import statements into the main sketch file (code[0]) // if the current code is a .java file, insert into current // if (current.flavor == PDE) { if (mode.isDefaultExtension(sketch.getCurrentCode())) { sketch.setCurrentCode(0); } // could also scan the text in the file to see if each import // statement is already in there, but if the user has the import // commented out, then this will be a problem. String[] list = Base.packageListFromClassPath(jarPath); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < list.length; i++) { buffer.append("import "); buffer.append(list[i]); buffer.append(".*;\n"); } buffer.append('\n'); buffer.append(getText()); setText(buffer.toString()); setSelection(0, 0); // scroll to start sketch.setModified(true); }
public EditorLineStatus(JEditTextArea textarea) { this.textarea = textarea; textarea.editorLineStatus = this; background = Theme.getColor("linestatus.bgcolor"); font = Theme.getFont("linestatus.font"); foreground = Theme.getColor("linestatus.color"); high = Theme.getInteger("linestatus.height"); if (Base.isMacOS()) { resize = Base.getThemeImage("resize.gif", this); } // linestatus.bgcolor = #000000 // linestatus.font = SansSerif,plain,10 // linestatus.color = #FFFFFF }
public EditorButtons(Editor editor) { this.editor = editor; buttons = Base.getImage("buttons.gif", this); buttonCount = 0; which = new int[BUTTON_COUNT]; // which[buttonCount++] = NOTHING; which[buttonCount++] = RUN; which[buttonCount++] = STOP; which[buttonCount++] = NEW; which[buttonCount++] = OPEN; which[buttonCount++] = SAVE; which[buttonCount++] = EXPORT; which[buttonCount++] = SERIAL; currentRollover = -1; // hardcoding new blue color scheme for consistency with images, // see EditorStatus.java for details. // bgcolor = Preferences.getColor("buttons.bgcolor"); bgcolor = new Color(0x04, 0x4F, 0x6F); status = ""; statusFont = Preferences.getFont("buttons.status.font"); statusColor = Preferences.getColor("buttons.status.color"); // statusY = (BUTTON_COUNT + 1) * BUTTON_HEIGHT; addMouseListener(this); addMouseMotionListener(this); }
/** Add import statements to the current tab for the specified library */ public void importLibrary(UserLibrary lib) throws IOException { // make sure the user didn't hide the sketch folder ensureExistence(); List<String> list = lib.getIncludes(); if (list == null) { File srcFolder = lib.getSrcFolder(); String[] headers = Base.headerListFromIncludePath(srcFolder); list = Arrays.asList(headers); } if (list.isEmpty()) { return; } // import statements into the main sketch file (code[0]) // if the current code is a .java file, insert into current // if (current.flavor == PDE) { SketchFile file = editor.getCurrentTab().getSketchFile(); if (file.isExtension(Sketch.SKETCH_EXTENSIONS)) editor.selectTab(0); // could also scan the text in the file to see if each import // statement is already in there, but if the user has the import // commented out, then this will be a problem. StringBuilder buffer = new StringBuilder(); for (String aList : list) { buffer.append("#include <"); buffer.append(aList); buffer.append(">\n"); } buffer.append('\n'); buffer.append(editor.getCurrentTab().getText()); editor.getCurrentTab().setText(buffer.toString()); editor.getCurrentTab().setSelection(0, 0); // scroll to start }
/** Save all code in the current sketch. */ public boolean save() throws IOException { // make sure the user didn't hide the sketch folder ensureExistence(); if (isReadOnly( BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) { Base.showMessage( tr("Sketch is read-only"), tr( "Some files are marked \"read-only\", so you'll\n" + "need to re-save this sketch to another location.")); return saveAs(); } // rename .pde files to .ino List<SketchFile> oldFiles = new ArrayList<>(); for (SketchFile file : sketch.getFiles()) { if (file.isExtension(Sketch.OLD_SKETCH_EXTENSIONS)) oldFiles.add(file); } if (oldFiles.size() > 0) { if (PreferencesData.get("editor.update_extension") == null) { Object[] options = {tr("OK"), tr("Cancel")}; int result = JOptionPane.showOptionDialog( editor, tr( "In Arduino 1.0, the default file extension has changed\n" + "from .pde to .ino. New sketches (including those created\n" + "by \"Save-As\") will use the new extension. The extension\n" + "of existing sketches will be updated on save, but you can\n" + "disable this in the Preferences dialog.\n" + "\n" + "Save sketch and update its extension?"), tr(".pde -> .ino"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (result != JOptionPane.OK_OPTION) return false; // save cancelled PreferencesData.setBoolean("editor.update_extension", true); } if (PreferencesData.getBoolean("editor.update_extension")) { // Do rename of all .pde files to new .ino extension for (SketchFile file : oldFiles) { File newName = FileUtils.replaceExtension(file.getFile(), Sketch.DEFAULT_SKETCH_EXTENSION); file.renameTo(newName.getName()); } } } sketch.save(); return true; }
protected void loadButtons() { Image allButtons = Base.getThemeImage("buttons.gif", this); buttonImages = new Image[BUTTON_COUNT][3]; for (int i = 0; i < BUTTON_COUNT; i++) { for (int state = 0; state < 3; state++) { Image image = createImage(BUTTON_WIDTH, BUTTON_HEIGHT); Graphics g = image.getGraphics(); g.drawImage(allButtons, -(i*BUTTON_IMAGE_SIZE) - 3, (-2 + state)*BUTTON_IMAGE_SIZE, null); buttonImages[i][state] = image; } } }
/** Handler for Sketch → Export Application */ public void handleExportApplication() { toolbar.activate(JavaToolbar.EXPORT); if (handleExportCheckModified()) { statusNotice("Exporting application..."); try { if (exportApplicationPrompt()) { Base.openFolder(sketch.getFolder()); statusNotice("Done exporting."); } else { // error message will already be visible // or there was no error, in which case it was canceled. } } catch (Exception e) { statusNotice("Error during export."); e.printStackTrace(); } } toolbar.deactivate(JavaToolbar.EXPORT); }
/** * Prompt the user for a new file to the sketch, then call the other addFile() function to * actually add it. */ public void handleAddFile() { // make sure the user didn't hide the sketch folder ensureExistence(); // if read-only, give an error if (isReadOnly( BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) { // if the files are read-only, need to first do a "save as". Base.showMessage( tr("Sketch is Read-Only"), tr( "Some files are marked \"read-only\", so you'll\n" + "need to re-save the sketch in another location,\n" + "and try again.")); return; } // get a dialog, select a file to add to the sketch FileDialog fd = new FileDialog( editor, tr("Select an image or other data file to copy to your sketch"), FileDialog.LOAD); fd.setVisible(true); String directory = fd.getDirectory(); String filename = fd.getFile(); if (filename == null) return; // copy the file into the folder. if people would rather // it move instead of copy, they can do it by hand File sourceFile = new File(directory, filename); // now do the work of adding the file boolean result = addFile(sourceFile); if (result) { editor.statusNotice(tr("One file added to the sketch.")); PreferencesData.set("last.folder", sourceFile.getAbsolutePath()); } }
/** Handler for the New Code menu option. */ public void handleNewCode() { editor.status.clearState(); // make sure the user didn't hide the sketch folder ensureExistence(); // if read-only, give an error if (isReadOnly( BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) { // if the files are read-only, need to first do a "save as". Base.showMessage( tr("Sketch is Read-Only"), tr( "Some files are marked \"read-only\", so you'll\n" + "need to re-save the sketch in another location,\n" + "and try again.")); return; } renamingCode = false; editor.status.edit(tr("Name for new file:"), ""); }
static protected void init(String commandLinePrefs) { // start by loading the defaults, in case something // important was deleted from the user prefs try { load(Base.getLibStream("preferences.txt")); } catch (Exception e) { Base.showError(null, _("Could not read default settings.\n" + "You'll need to reinstall Arduino."), e); } // check for platform-specific properties in the defaults String platformExt = "." + PConstants.platformNames[PApplet.platform]; int platformExtLength = platformExt.length(); Enumeration e = table.keys(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); if (key.endsWith(platformExt)) { // this is a key specific to a particular platform String actualKey = key.substring(0, key.length() - platformExtLength); String value = get(key); table.put(actualKey, value); } } // clone the hash table defaults = (Hashtable) table.clone(); // other things that have to be set explicitly for the defaults setColor("run.window.bgcolor", SystemColor.control); // Load a prefs file if specified on the command line if (commandLinePrefs != null) { try { load(new FileInputStream(commandLinePrefs)); } catch (Exception poe) { Base.showError(_("Error"), I18n.format( _("Could not read preferences from {0}"), commandLinePrefs ), poe); } } else if (!Base.isCommandLine()) { // next load user preferences file preferencesFile = Base.getSettingsFile(PREFS_FILE); if (!preferencesFile.exists()) { // create a new preferences file if none exists // saves the defaults out to the file save(); } else { // load the previous preferences file try { load(new FileInputStream(preferencesFile)); } catch (Exception ex) { Base.showError(_("Error reading preferences"), I18n.format( _("Error reading the preferences file. " + "Please delete (or move)\n" + "{0} and restart Arduino."), preferencesFile.getAbsolutePath() ), ex); } } } }
/** * Add a file to the sketch. * * <p>Supported code files will be copied to the sketch folder. All other files will be copied to * the "data" folder (which is created if it does not exist yet). * * @return true if successful. */ public boolean addFile(File sourceFile) { String filename = sourceFile.getName(); File destFile = null; boolean isData = false; boolean replacement = false; if (FileUtils.hasExtension(sourceFile, Sketch.EXTENSIONS)) { destFile = new File(sketch.getFolder(), filename); } else { sketch.prepareDataFolder(); destFile = new File(sketch.getDataFolder(), filename); isData = true; } // check whether this file already exists if (destFile.exists()) { Object[] options = {tr("OK"), tr("Cancel")}; String prompt = I18n.format(tr("Replace the existing version of {0}?"), filename); int result = JOptionPane.showOptionDialog( editor, prompt, tr("Replace"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (result == JOptionPane.YES_OPTION) { replacement = true; } else { return false; } } // If it's a replacement, delete the old file first, // otherwise case changes will not be preserved. // http://dev.processing.org/bugs/show_bug.cgi?id=969 if (replacement) { boolean muchSuccess = destFile.delete(); if (!muchSuccess) { Base.showWarning( tr("Error adding file"), I18n.format(tr("Could not delete the existing ''{0}'' file."), filename), null); return false; } } // make sure they aren't the same file if (isData && sourceFile.equals(destFile)) { Base.showWarning( tr("You can't fool me"), tr( "This file has already been copied to the\n" + "location from which where you're trying to add it.\n" + "I ain't not doin nuthin'."), null); return false; } // in case the user is "adding" the code in an attempt // to update the sketch's tabs if (!sourceFile.equals(destFile)) { try { Base.copyFile(sourceFile, destFile); } catch (IOException e) { Base.showWarning( tr("Error adding file"), I18n.format(tr("Could not add ''{0}'' to the sketch."), filename), e); return false; } } if (!isData) { int tabIndex; if (replacement) { tabIndex = editor.findTabIndex(destFile); editor.getTabs().get(tabIndex).reload(); } else { SketchFile sketchFile; try { sketchFile = sketch.addFile(destFile.getName()); editor.addTab(sketchFile, null); } catch (IOException e) { // This does not pass on e, to prevent showing a backtrace for // "normal" errors. Base.showWarning(tr("Error"), e.getMessage(), null); return false; } tabIndex = editor.findTabIndex(sketchFile); } editor.selectTab(tabIndex); } return true; }
/** * Handles 'Save As' for a sketch. * * <p>This basically just duplicates the current sketch folder to a new location, and then calls * 'Save'. (needs to take the current state of the open files and save them to the new folder.. * but not save over the old versions for the old sketch..) * * <p>Also removes the previously-generated .class and .jar files, because they can cause trouble. */ protected boolean saveAs() throws IOException { // get new name for folder FileDialog fd = new FileDialog(editor, tr("Save sketch folder as..."), FileDialog.SAVE); if (isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath()) || isUntitled()) { // default to the sketchbook folder fd.setDirectory(BaseNoGui.getSketchbookFolder().getAbsolutePath()); } else { // default to the parent folder of where this was // on macs a .getParentFile() method is required fd.setDirectory(sketch.getFolder().getParentFile().getAbsolutePath()); } String oldName = sketch.getName(); fd.setFile(oldName); fd.setVisible(true); String newParentDir = fd.getDirectory(); String newName = fd.getFile(); // user canceled selection if (newName == null) return false; newName = SketchController.checkName(newName); File newFolder = new File(newParentDir, newName); // check if the paths are identical if (newFolder.equals(sketch.getFolder())) { // just use "save" here instead, because the user will have received a // message (from the operating system) about "do you want to replace?" return save(); } // check to see if the user is trying to save this sketch inside itself try { String newPath = newFolder.getCanonicalPath() + File.separator; String oldPath = sketch.getFolder().getCanonicalPath() + File.separator; if (newPath.indexOf(oldPath) == 0) { Base.showWarning( tr("How very Borges of you"), tr( "You cannot save the sketch into a folder\n" + "inside itself. This would go on forever."), null); return false; } } catch (IOException e) { // ignore } // if the new folder already exists, then need to remove // its contents before copying everything over // (user will have already been warned) if (newFolder.exists()) { FileUtils.recursiveDelete(newFolder); } // in fact, you can't do this on windows because the file dialog // will instead put you inside the folder, but it happens on osx a lot. try { sketch.saveAs(newFolder); } catch (IOException e) { // This does not pass on e, to prevent showing a backtrace for "normal" // errors. Base.showWarning(tr("Error"), e.getMessage(), null); } // Name changed, rebuild the sketch menus // editor.sketchbook.rebuildMenusAsync(); editor.base.rebuildSketchbookMenus(); editor.header.rebuild(); // Make sure that it's not an untitled sketch setUntitled(false); // let Editor know that the save was successful return true; }
public Preferences() { // setup dialog for the prefs //dialog = new JDialog(editor, "Preferences", true); dialog = new JFrame(_("Preferences")); dialog.setResizable(false); Container pain = dialog.getContentPane(); pain.setLayout(null); int top = GUI_BIG; int left = GUI_BIG; int right = 0; JLabel label; JButton button; //, button2; //JComboBox combo; Dimension d, d2; //, d3; int h, vmax; // Sketchbook location: // [...............................] [ Browse ] label = new JLabel(_("Sketchbook location:")); pain.add(label); d = label.getPreferredSize(); label.setBounds(left, top, d.width, d.height); top += d.height; // + GUI_SMALL; sketchbookLocationField = new JTextField(40); pain.add(sketchbookLocationField); d = sketchbookLocationField.getPreferredSize(); button = new JButton(PROMPT_BROWSE); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { File dflt = new File(sketchbookLocationField.getText()); File file = Base.selectFolder(_("Select new sketchbook location"), dflt, dialog); if (file != null) { sketchbookLocationField.setText(file.getAbsolutePath()); } } }); pain.add(button); d2 = button.getPreferredSize(); // take max height of all components to vertically align em vmax = Math.max(d.height, d2.height); sketchbookLocationField.setBounds(left, top + (vmax-d.height)/2, d.width, d.height); h = left + d.width + GUI_SMALL; button.setBounds(h, top + (vmax-d2.height)/2, d2.width, d2.height); right = Math.max(right, h + d2.width + GUI_BIG); top += vmax + GUI_BETWEEN; // Preferred language: [ ] (requires restart of Arduino) Container box = Box.createHorizontalBox(); label = new JLabel(_("Editor language: ")); box.add(label); comboLanguage = new JComboBox(languages); comboLanguage.setSelectedIndex((Arrays.asList(languagesISO)).indexOf(Preferences.get("editor.languages.current"))); box.add(comboLanguage); label = new JLabel(_(" (requires restart of Arduino)")); box.add(label); pain.add(box); d = box.getPreferredSize(); box.setForeground(Color.gray); box.setBounds(left, top, d.width, d.height); right = Math.max(right, left + d.width); top += d.height + GUI_BETWEEN; // Editor font size [ ] box = Box.createHorizontalBox(); label = new JLabel(_("Editor font size: ")); box.add(label); fontSizeField = new JTextField(4); box.add(fontSizeField); label = new JLabel(_(" (requires restart of Arduino)")); box.add(label); pain.add(box); d = box.getPreferredSize(); box.setBounds(left, top, d.width, d.height); Font editorFont = Preferences.getFont("editor.font"); fontSizeField.setText(String.valueOf(editorFont.getSize())); top += d.height + GUI_BETWEEN; // Show verbose output during: [ ] compilation [ ] upload box = Box.createHorizontalBox(); label = new JLabel(_("Show verbose output during: ")); box.add(label); verboseCompilationBox = new JCheckBox(_("compilation ")); box.add(verboseCompilationBox); verboseUploadBox = new JCheckBox(_("upload")); box.add(verboseUploadBox); pain.add(box); d = box.getPreferredSize(); box.setBounds(left, top, d.width, d.height); top += d.height + GUI_BETWEEN; // [ ] Verify code after upload verifyUploadBox = new JCheckBox(_("Verify code after upload")); pain.add(verifyUploadBox); d = verifyUploadBox.getPreferredSize(); verifyUploadBox.setBounds(left, top, d.width + 10, d.height); right = Math.max(right, left + d.width); top += d.height + GUI_BETWEEN; // [ ] Use external editor externalEditorBox = new JCheckBox(_("Use external editor")); pain.add(externalEditorBox); d = externalEditorBox.getPreferredSize(); externalEditorBox.setBounds(left, top, d.width + 10, d.height); right = Math.max(right, left + d.width); top += d.height + GUI_BETWEEN; // [ ] Check for updates on startup checkUpdatesBox = new JCheckBox(_("Check for updates on startup")); pain.add(checkUpdatesBox); d = checkUpdatesBox.getPreferredSize(); checkUpdatesBox.setBounds(left, top, d.width + 10, d.height); right = Math.max(right, left + d.width); top += d.height + GUI_BETWEEN; // [ ] Update sketch files to new extension on save (.pde -> .ino) updateExtensionBox = new JCheckBox(_("Update sketch files to new extension on save (.pde -> .ino)")); pain.add(updateExtensionBox); d = updateExtensionBox.getPreferredSize(); updateExtensionBox.setBounds(left, top, d.width + 10, d.height); right = Math.max(right, left + d.width); top += d.height + GUI_BETWEEN; domainPortField= new JTextField(); domainPortField.setColumns(30); Box domainBox = Box.createHorizontalBox(); domainBox.add(new JLabel("Tftp upload Domain:")); domainBox.add(domainPortField); pain.add(domainBox); d = domainBox.getPreferredSize(); domainBox.setBounds(left, top, d.width, d.height); top += d.height + GUI_BETWEEN; autoResetPortField= new JTextField(); autoResetPortField.setColumns(8); Box resetBox = Box.createHorizontalBox(); resetBox.add(new JLabel("Auto Reset Port:")); resetBox.add(autoResetPortField); pain.add(resetBox); d = resetBox.getPreferredSize(); resetBox.setBounds(left, top, d.width, d.height); top += d.height + GUI_BETWEEN; tftpPassField = new JTextField(); tftpPassField.setColumns(30); Box tftpBox = Box.createHorizontalBox(); tftpBox.add(new JLabel("Tftp Secret Password:"******"Automatically associate .ino files with Arduino")); pain.add(autoAssociateBox); d = autoAssociateBox.getPreferredSize(); autoAssociateBox.setBounds(left, top, d.width + 10, d.height); right = Math.max(right, left + d.width); top += d.height + GUI_BETWEEN; } // More preferences are in the ... label = new JLabel(_("More preferences can be edited directly in the file")); pain.add(label); d = label.getPreferredSize(); label.setForeground(Color.gray); label.setBounds(left, top, d.width, d.height); right = Math.max(right, left + d.width); top += d.height; // + GUI_SMALL; label = new JLabel(preferencesFile.getAbsolutePath()); final JLabel clickable = label; label.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { Base.openFolder(Base.getSettingsFolder()); } public void mouseEntered(MouseEvent e) { clickable.setForeground(new Color(0, 0, 140)); } public void mouseExited(MouseEvent e) { clickable.setForeground(Color.BLACK); } }); pain.add(label); d = label.getPreferredSize(); label.setBounds(left, top, d.width, d.height); right = Math.max(right, left + d.width); top += d.height; label = new JLabel(_("(edit only when Arduino is not running)")); pain.add(label); d = label.getPreferredSize(); label.setForeground(Color.gray); label.setBounds(left, top, d.width, d.height); right = Math.max(right, left + d.width); top += d.height; // + GUI_SMALL; // [ OK ] [ Cancel ] maybe these should be next to the message? button = new JButton(PROMPT_OK); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { applyFrame(); disposeFrame(); } }); pain.add(button); d2 = button.getPreferredSize(); BUTTON_HEIGHT = d2.height; h = right - (BUTTON_WIDTH + GUI_SMALL + BUTTON_WIDTH); button.setBounds(h, top, BUTTON_WIDTH, BUTTON_HEIGHT); h += BUTTON_WIDTH + GUI_SMALL; button = new JButton(PROMPT_CANCEL); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { disposeFrame(); } }); pain.add(button); button.setBounds(h, top, BUTTON_WIDTH, BUTTON_HEIGHT); top += BUTTON_HEIGHT + GUI_BETWEEN; // finish up wide = right + GUI_BIG; high = top + GUI_SMALL; // closing the window is same as hitting cancel button dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { disposeFrame(); } }); ActionListener disposer = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { disposeFrame(); } }; Base.registerWindowCloseKeys(dialog.getRootPane(), disposer); Base.setIcon(dialog); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); dialog.setLocation((screen.width - wide) / 2, (screen.height - high) / 2); dialog.pack(); // get insets Insets insets = dialog.getInsets(); dialog.setSize(wide + insets.left + insets.right, high + insets.top + insets.bottom); // handle window closing commands for ctrl/cmd-W or hitting ESC. pain.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { //System.out.println(e); KeyStroke wc = Editor.WINDOW_CLOSE_KEYSTROKE; if ((e.getKeyCode() == KeyEvent.VK_ESCAPE) || (KeyStroke.getKeyStrokeForEvent(e).equals(wc))) { disposeFrame(); } } }); }
/** * This is called upon return from entering a new file name. (that is, from either newCode or * renameCode after the prompt) This code is almost identical for both the newCode and renameCode * cases, so they're kept merged except for right in the middle where they diverge. */ protected void nameCode(String newName) { // make sure the user didn't hide the sketch folder ensureExistence(); newName = newName.trim(); if (newName.equals("")) return; if (newName.charAt(0) == '.') { Base.showWarning(tr("Problem with rename"), tr("The name cannot start with a period."), null); return; } FileUtils.SplitFile split = FileUtils.splitFilename(newName); if (split.extension.equals("")) split.extension = Sketch.DEFAULT_SKETCH_EXTENSION; if (!Sketch.EXTENSIONS.contains(split.extension)) { String msg = I18n.format(tr("\".{0}\" is not a valid extension."), split.extension); Base.showWarning(tr("Problem with rename"), msg, null); return; } // Sanitize name split.basename = BaseNoGui.sanitizeName(split.basename); newName = split.join(); if (renamingCode) { SketchFile current = editor.getCurrentTab().getSketchFile(); if (current.isPrimary()) { if (!split.extension.equals(Sketch.DEFAULT_SKETCH_EXTENSION)) { Base.showWarning( tr("Problem with rename"), tr("The main file cannot use an extension"), null); return; } // Primary file, rename the entire sketch final File parent = sketch.getFolder().getParentFile(); File newFolder = new File(parent, split.basename); try { sketch.renameTo(newFolder); } catch (IOException e) { // This does not pass on e, to prevent showing a backtrace for // "normal" errors. Base.showWarning(tr("Error"), e.getMessage(), null); return; } editor.base.rebuildSketchbookMenus(); } else { // Non-primary file, rename just that file try { current.renameTo(newName); } catch (IOException e) { // This does not pass on e, to prevent showing a backtrace for // "normal" errors. Base.showWarning(tr("Error"), e.getMessage(), null); return; } } } else { // creating a new file SketchFile file; try { file = sketch.addFile(newName); editor.addTab(file, ""); } catch (IOException e) { // This does not pass on e, to prevent showing a backtrace for // "normal" errors. Base.showWarning(tr("Error"), e.getMessage(), null); return; } editor.selectTab(editor.findTabIndex(file)); } // update the tabs editor.header.rebuild(); }
public JMenu buildHelpMenu() { // To deal with a Mac OS X 10.5 bug, add an extra space after the name // so that the OS doesn't try to insert its slow help menu. JMenu menu = new JMenu("Help "); JMenuItem item; // macosx already has its own about menu if (!Base.isMacOS()) { item = new JMenuItem("About Processing"); item.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { new About(JavaEditor.this); } }); menu.add(item); } item = new JMenuItem("Environment"); item.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { showReference("environment" + File.separator + "index.html"); } }); menu.add(item); item = new JMenuItem("Reference"); item.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { showReference("index.html"); } }); menu.add(item); item = Toolkit.newJMenuItemShift("Find in Reference", 'F'); item.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (textarea.isSelectionActive()) { handleFindReference(); } } }); menu.add(item); menu.addSeparator(); item = new JMenuItem("Online"); item.setEnabled(false); menu.add(item); item = new JMenuItem("Getting Started"); item.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Base.openURL("http://processing.org/learning/gettingstarted/"); } }); menu.add(item); item = new JMenuItem("Troubleshooting"); item.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Base.openURL("http://wiki.processing.org/w/Troubleshooting"); } }); menu.add(item); item = new JMenuItem("Frequently Asked Questions"); item.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Base.openURL("http://wiki.processing.org/w/FAQ"); } }); menu.add(item); item = new JMenuItem("Visit Processing.org"); item.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Base.openURL("http://processing.org/"); } }); menu.add(item); return menu; }