public void showItem() { if (!this.editorPanel.getViewer().isLanguageFunctionAvailable()) { return; } this.highlight = this.editorPanel .getEditor() .addHighlight(this.position, this.position + this.word.length(), null, true); final FindSynonymsActionHandler _this = this; QTextEditor editor = this.editorPanel.getEditor(); Rectangle r = null; try { r = editor.modelToView(this.position); } catch (Exception e) { // BadLocationException! Environment.logError("Location: " + this.position + " is not valid", e); UIUtils.showErrorMessage(this.editorPanel, "Unable to display synonyms."); return; } int y = r.y; // Show a panel of all the items. final QPopup p = this.popup; p.setOpaque(false); Synonyms syns = null; try { syns = this.projectViewer.getSynonymProvider().getSynonyms(this.word); } catch (Exception e) { UIUtils.showErrorMessage(this.editorPanel, "Unable to display synonyms."); Environment.logError("Unable to lookup synonyms for: " + word, e); return; } if ((syns.words.size() == 0) && (this.word.toLowerCase().endsWith("ed"))) { // Trim off the ed and try again. try { syns = this.projectViewer.getSynonyms(this.word.substring(0, this.word.length() - 2)); } catch (Exception e) { UIUtils.showErrorMessage(this.editorPanel, "Unable to display synonyms."); Environment.logError("Unable to lookup synonyms for: " + word, e); return; } } if ((syns.words.size() == 0) && (this.word.toLowerCase().endsWith("s"))) { // Trim off the ed and try again. try { syns = this.projectViewer.getSynonyms(this.word.substring(0, this.word.length() - 1)); } catch (Exception e) { UIUtils.showErrorMessage(this.editorPanel, "Unable to display synonyms."); Environment.logError("Unable to lookup synonyms for: " + word, e); return; } } StringBuilder sb = new StringBuilder(); if (syns.words.size() > 0) { sb.append("6px"); for (int i = 0; i < syns.words.size(); i++) { if (sb.length() > 0) { sb.append(", "); } sb.append("p, 3px, [p,90px], 5px"); } /* if (syns.words.size () > 0) { sb.append (",5px"); } */ } else { sb.append("6px, p, 6px"); } FormLayout summOnly = new FormLayout("3px, fill:380px:grow, 3px", sb.toString()); PanelBuilder pb = new PanelBuilder(summOnly); CellConstraints cc = new CellConstraints(); int ind = 2; Map<String, String> names = new HashMap(); names.put(Synonyms.ADJECTIVE + "", "Adjectives"); names.put(Synonyms.NOUN + "", "Nouns"); names.put(Synonyms.VERB + "", "Verbs"); names.put(Synonyms.ADVERB + "", "Adverbs"); names.put(Synonyms.OTHER + "", "Other"); if (syns.words.size() == 0) { JLabel l = new JLabel("No synonyms found."); l.setFont(l.getFont().deriveFont(Font.ITALIC)); pb.add(l, cc.xy(2, 2)); } // Determine what type of word we are looking for. for (Synonyms.Part i : syns.words) { JLabel l = new JLabel(names.get(i.type + "")); l.setFont(l.getFont().deriveFont(Font.ITALIC)); l.setFont(l.getFont().deriveFont((float) UIUtils.getEditorFontSize(10))); l.setBorder( new CompoundBorder( new MatteBorder(0, 0, 1, 0, Environment.getBorderColor()), new EmptyBorder(0, 0, 3, 0))); pb.add(l, cc.xy(2, ind)); ind += 2; HTMLEditorKit kit = new HTMLEditorKit(); HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); JTextPane t = new JTextPane(doc); t.setEditorKit(kit); t.setEditable(false); t.setOpaque(false); StringBuilder buf = new StringBuilder( "<style>a { text-decoration: none; } a:hover { text-decoration: underline; }</style><span style='color: #000000; font-size: " + ((int) UIUtils.getEditorFontSize(10) /*t.getFont ().getSize () + 2*/) + "pt; font-family: " + t.getFont().getFontName() + ";'>"); for (int x = 0; x < i.words.size(); x++) { String w = (String) i.words.get(x); buf.append("<a class='x' href='http://" + w + "'>" + w + "</a>"); if (x < (i.words.size() - 1)) { buf.append(", "); } } buf.append("</span>"); t.setText(buf.toString()); t.addHyperlinkListener( new HyperlinkAdapter() { public void hyperlinkUpdate(HyperlinkEvent ev) { if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { QTextEditor ed = _this.editorPanel.getEditor(); ed.replaceText( _this.position, _this.position + _this.word.length(), ev.getURL().getHost()); ed.removeHighlight(_this.highlight); _this.popup.setVisible(false); _this.projectViewer.fireProjectEvent( ProjectEvent.SYNONYM, ProjectEvent.REPLACE, ev.getURL().getHost()); } } }); // Annoying that we have to do this but it prevents the text from being too small. t.setSize(new Dimension(380, Short.MAX_VALUE)); JScrollPane sp = new JScrollPane(t); t.setCaretPosition(0); sp.setOpaque(false); sp.getVerticalScrollBar().setValue(0); /* sp.setPreferredSize (t.getPreferredSize ()); sp.setMaximumSize (new Dimension (380, 75)); */ sp.getViewport().setOpaque(false); sp.setOpaque(false); sp.setBorder(null); sp.getViewport().setBackground(Color.WHITE); sp.setAlignmentX(Component.LEFT_ALIGNMENT); pb.add(sp, cc.xy(2, ind)); ind += 2; } JPanel pan = pb.getPanel(); pan.setOpaque(true); pan.setBackground(Color.WHITE); this.popup.setContent(pan); // r.y -= this.editorPanel.getScrollPane ().getVerticalScrollBar ().getValue (); Point po = SwingUtilities.convertPoint(editor, r.x, r.y, this.editorPanel); r.x = po.x; r.y = po.y; // Subtract the insets of the editorPanel. Insets ins = this.editorPanel.getInsets(); r.x -= ins.left; r.y -= ins.top; this.editorPanel.showPopupAt(this.popup, r, "above", true); }
// Remove a test case from testBox and tests array. private void removeTest() { // Make sure they want to remove the checked tests. String message = "Are you sure you would like to remove the selected tests?"; int decision = JOptionPane.showConfirmDialog(null, message, "Remove", JOptionPane.OK_CANCEL_OPTION); // If they definitely do want to remove. if (decision == JOptionPane.OK_OPTION) { // Make array list copy, so as to avoid index errors. ArrayList<JCheckBox> newTests = (ArrayList<JCheckBox>) tests.clone(); int removed = 0; for (int i = 0; i < tests.size(); i++) { if (tests.get(i).isSelected()) { // Remove from the tests copy. newTests.remove(i - removed); // Remove from the GUI box. testBox.setEditable(true); testBox.remove(tests.get(i)); testBox.repaint(); testBox.setEditable(false); } } // Update the tests with the newly made list. tests = newTests; } }
protected void buildErrorPanel() { errorPanel = new JPanel(); GroupLayout layout = new GroupLayout(errorPanel); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); errorPanel.setLayout(layout); // errorPanel.setBorder(BorderFactory.createMatteBorder(2, 0, 0, 0, Color.BLACK)); errorMessage = new JTextPane(); errorMessage.setEditable(false); errorMessage.setContentType("text/html"); errorMessage.setText( "<html><body>Could not connect to the Processing server.<br>" + "Contributions cannot be installed or updated without an Internet connection.<br>" + "Please verify your network connection again, then try connecting again.</body></html>"); errorMessage.setFont(Toolkit.getSansFont(14, Font.PLAIN)); errorMessage.setMaximumSize(new Dimension(550, 50)); errorMessage.setOpaque(false); StyledDocument doc = errorMessage.getStyledDocument(); SimpleAttributeSet center = new SimpleAttributeSet(); StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); doc.setParagraphAttributes(0, doc.getLength(), center, false); closeButton = new JButton("X"); closeButton.setContentAreaFilled(false); closeButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { contribDialog.makeAndShowTab(false, false); } }); tryAgainButton = new JButton("Try Again"); tryAgainButton.setFont(Toolkit.getSansFont(14, Font.PLAIN)); tryAgainButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { contribDialog.makeAndShowTab(false, true); contribDialog.downloadAndUpdateContributionListing(editor.getBase()); } }); layout.setHorizontalGroup( layout .createSequentialGroup() .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE) .addGroup( layout .createParallelGroup(GroupLayout.Alignment.CENTER) .addComponent(errorMessage) .addComponent( tryAgainButton, StatusPanel.BUTTON_WIDTH, StatusPanel.BUTTON_WIDTH, StatusPanel.BUTTON_WIDTH)) .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE) .addComponent(closeButton)); layout.setVerticalGroup( layout .createSequentialGroup() .addGroup( layout.createParallelGroup().addComponent(errorMessage).addComponent(closeButton)) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(tryAgainButton)); errorPanel.setBackground(Color.PINK); errorPanel.validate(); }
private JTextPane makeTextPane(boolean editable) { document = new DefaultStyledDocument(); JTextPane ta = new JTextPane(document); ta.setEditable(editable); return ta; }
public static void main() { // Main frame = new JFrame("Java Playground"); frame.setSize(640, 480); // Make sure the divider is properly resized frame.addComponentListener( new ComponentAdapter() { public void componentResized(ComponentEvent c) { splitter.setDividerLocation(.8); } }); // Make sure the JVM is reset on close frame.addWindowListener( new WindowAdapter() { public void windowClosed(WindowEvent w) { new FrameAction().kill(); } }); // Setting up the keybinding // Ctrl+k or Cmd+k -> compile bind(KeyEvent.VK_K); // Ctrl+e or Cmd+e -> console bind(KeyEvent.VK_E); // Save, New file, Open file, Print. // Currently UNUSED until I figure out how normal java files and playground files will // interface. bind(KeyEvent.VK_S); bind(KeyEvent.VK_N); bind(KeyEvent.VK_O); bind(KeyEvent.VK_P); // Binds the keys to the action defined in the FrameAction class. frame.getRootPane().getActionMap().put("console", new FrameAction()); // The main panel for typing code in. text = new JTextPane(); textScroll = new JScrollPane(text); textScroll.setBorder(null); textScroll.setPreferredSize(new Dimension(640, 480)); // Document with syntax highlighting. Currently unfinished. doc = text.getStyledDocument(); doc.addDocumentListener( new DocumentListener() { public void changedUpdate(DocumentEvent d) {} public void insertUpdate(DocumentEvent d) {} public void removeUpdate(DocumentEvent d) {} }); ((AbstractDocument) doc).setDocumentFilter(new NewLineFilter()); // The output log; a combination compiler warning/error/runtime error/output log. outputText = new JTextPane(); outputScroll = new JScrollPane(outputText); outputScroll.setBorder(null); // "Constant" for the error font error = new SimpleAttributeSet(); error.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE); error.addAttribute(StyleConstants.Foreground, Color.RED); // "Constant" for the warning message font warning = new SimpleAttributeSet(); warning.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE); warning.addAttribute(StyleConstants.Foreground, Color.PINK); // "Constant" for the debugger error font progErr = new SimpleAttributeSet(); progErr.addAttribute(StyleConstants.Foreground, Color.BLUE); // Print streams to redirect System.out and System.err. out = new TextOutputStream(outputText, null); err = new TextOutputStream(outputText, error); System.setOut(new PrintStream(out)); System.setErr(new PrintStream(err)); // Sets up the output log outputText.setEditable(false); outputScroll.setVisible(true); // File input/output setup chooser = new JFileChooser(); // Setting up miscellaneous stuff compiler = ToolProvider.getSystemJavaCompiler(); JVMrunning = false; redirectErr = null; redirectOut = null; redirectIn = null; // Sets up the splitter pane and opens the program up splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT, textScroll, outputScroll); consoleDisplayed = false; splitter.remove(outputScroll); // Initially hides terminal until it is needed splitter.setOneTouchExpandable(true); frame.add(splitter); frame.setVisible(true); // Sets the divider to the proper one, for debugging // splitter.setDividerLocation(.8); }
/** Initialize the user interface components. */ protected void initUI() throws Exception { // Setup the JInternalFrame setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE); addInternalFrameListener(new DialogListener()); // grab Frog object Frog frame = debugManager.getFrog(); // grab location and position window Dimension popupSize = getSize(); Dimension frameSize = frame.getSize(); setLocation((frameSize.width - popupSize.width) / 4, (frameSize.height - popupSize.height) / 4); // Grab TimeSeriesComp object GramComp popupComp = gramManager.getGram(popupFrame); Gram popupGram = popupComp.getGram(); // create the main panel // --------------------- // setup the scroll pane JScrollPane scrollPane = new JScrollPane( textPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollPane.setPreferredSize(new Dimension(500, 450)); scrollPane.setMinimumSize(new Dimension(500, 300)); textPane.setEditable(false); textPane.setContentType("text/html"); // Stuff them into the main panel // ------------------------------ GridBagConstraints constraints = new GridBagConstraints(); constraints.weightx = 1.0; constraints.weighty = 1.0; constraints.fill = GridBagConstraints.BOTH; JPanel mainPanel = new JPanel(); // create the main panel mainPanel.setLayout(new GridBagLayout()); constraints.gridx = 0; constraints.gridy = 0; mainPanel.add(scrollPane, constraints); // create the button panels JPanel buttonPanel = new JPanel(new BorderLayout()); JPanel buttons = new JPanel(new BorderLayout()); // create the button panel // ----------------------- JLabel buttonLabel = new JLabel(" "); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } }); buttons.add(cancelButton, BorderLayout.EAST); buttonPanel.add(buttonLabel, BorderLayout.CENTER); buttonPanel.add(buttons, BorderLayout.EAST); // display everything JPanel contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(mainPanel, BorderLayout.NORTH); contentPane.add(buttonPanel, BorderLayout.SOUTH); // Add information to textArea // --------------------------- String doc = "<html><body>"; doc = doc + "<h2><u>Periodogram</u></h2>\n"; doc = doc + "<ul>\n"; TimeSeriesComp popupTimeComp = popupGram.getTimeSeriesComp(); TimeSeries popupSeries = popupTimeComp.getSeries(); String key = seriesManager.getKey(popupTimeComp); doc = doc + "<li>The periodogram is associated with <strong>" + key + "</strong>\n"; if (popupGram.getType() == Gram.UNCLASSIFIED) { doc = doc + "<li>The periodogram is of unknown type\n" + "<li><font color=red>Warning: Possible programming error?</font>\n" + "</ul>\n"; } else if (popupGram.getType() == Gram.FOURIER) { doc = doc + "<li>The periodogram is a Fourier Transform</ul>\n"; } else if (popupGram.getType() == Gram.CHISQ) { doc = doc + "<li>The periodogram is a Chi-Squared Periodogram</ul>\n"; } // best fit period? String periodDoc = ""; if (popupGram.haveBestPeriod()) { double range[] = popupGram.getRange(); periodDoc = "<h2><u>Best Period</u></h2>\n"; periodDoc = periodDoc + "<ul>\n" + " <li> Minimum Frequency is " + range[0] + "\n" + " <li> Maximum Frequency is " + range[1] + "\n" + " <li> Maximum power at frequency of " + 1.0 / popupGram.getBestPeriod() + "\n" + " <li> This corresponds to a period of " + popupGram.getBestPeriod() + "\n</ul>\n"; } // drop document into textArea doc = doc + periodDoc + "</body></html>"; debugManager.print(" Passing HTML Document to textPane..."); debugManager.print("\n" + doc + "\n"); textPane.setText(doc); // Pack now avoiding problems with the scrollbar? pack(); }
// Create the display inside the "Replay" tab. private JPanel createReplayPanel() { // Create the overall panel with a space between each component. JPanel panel = new JPanel(new BorderLayout(20, 20)); // Create container for the bottom buttons. JPanel bottomButtons = new JPanel(); bottomButtons.setLayout(new BoxLayout(bottomButtons, BoxLayout.X_AXIS)); // Create the buttons and label. JButton run = new JButton("Run"); run.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { runTests(); } }); JButton close = new JButton("Close"); close.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { closeProgram(); } }); JButton add = new JButton("Add"); add.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { addTest(); } }); JButton remove = new JButton("Remove"); remove.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { removeTest(); } }); JLabel testLabel = new JLabel("Tests:"); // Create the test box and make it scrollable. testBox = new JTextPane(); testBox.setLayout(new BoxLayout(testBox, BoxLayout.Y_AXIS)); testBox.setEditable(false); JScrollPane scroll = new JScrollPane(testBox); // Add the test label and box. panel.add(testLabel, BorderLayout.PAGE_START); panel.add(scroll, BorderLayout.CENTER); // Add the buttons to bottom container and add that to overall. bottomButtons.add(add); bottomButtons.add(Box.createRigidArea(new Dimension(10, 0))); bottomButtons.add(remove); bottomButtons.add(Box.createRigidArea(new Dimension(10, 0))); bottomButtons.add(run); bottomButtons.add(Box.createRigidArea(new Dimension(10, 0))); bottomButtons.add(close); panel.add(bottomButtons, BorderLayout.PAGE_END); // Add space in between edge of tab page. panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); return panel; }
// Create the display inside the "Capture" tab. private JPanel createCapturePanel() { // Create the overall panel with a space between each component. JPanel panel = new JPanel(new BorderLayout(20, 20)); // Create container for the bottom buttons. JPanel bottomButtons = new JPanel(); bottomButtons.setLayout(new BoxLayout(bottomButtons, BoxLayout.X_AXIS)); // Create the bottom buttons and top label. recordButton = new JButton("Record"); recordButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { record(); } }); stopButton = new JButton("Stop"); stopButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { stopRecord(); } }); JButton save = new JButton("Save Test"); save.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { saveTest(); } }); JButton close = new JButton("Close"); close.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { closeProgram(); } }); JLabel updateLabel = new JLabel("Recent Actions:"); // Create the update box and make it scrollable. updateBox = new JTextPane(); updateBox.setText("Welcome to the JFC Capture/Replay Tool!"); JScrollPane scroll = new JScrollPane(updateBox); updateBox.setEditable(false); // Add the update label and box. panel.add(updateLabel, BorderLayout.PAGE_START); panel.add(scroll, BorderLayout.CENTER); // Add the buttons to bottom container and add that to overall. bottomButtons.add(recordButton); bottomButtons.add(Box.createRigidArea(new Dimension(10, 0))); bottomButtons.add(stopButton); bottomButtons.add(Box.createRigidArea(new Dimension(10, 0))); bottomButtons.add(save); bottomButtons.add(Box.createRigidArea(new Dimension(10, 0))); bottomButtons.add(close); panel.add(bottomButtons, BorderLayout.PAGE_END); stopButton.setEnabled(false); // Add space in between edge of tab page. panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); return panel; }
public DocumentPanel() { super(new BorderLayout()); JLabel lblDocument = new JLabel("Document: " + document.getTitle()); lblDocument.setBorder(new EtchedBorder()); textPane = new JTextPane(document); textPane.setEditable(false); textPane.setMargin(new Insets(5, 20, 5, 5)); textPane.setMaximumSize(new Dimension(364, 1000000000)); textPane.setPreferredSize(new Dimension(364, 400)); textPane.setMinimumSize(new Dimension(364, 10)); textPane.addCaretListener( new CaretListener() { public void caretUpdate(CaretEvent e) { int length = document.getLength(); int offset = e.getDot(); if (e.getDot() == e.getMark()) textPane.getCaret().moveDot(offset + 1); Paragraph p = lockManager.getParFromOffset(offset); int pOffset = p.getOffset(); lblCursor.setText( "Document Length=" + String.valueOf(length) + ", CaretOffset=" + String.valueOf(offset) + ", Paragraph=" + p.toString() + ", Offset in Paragraph=" + String.valueOf(offset - p.getOffset())); } }); Box box = new Box(BoxLayout.X_AXIS); box.add(textPane); box.add(Box.createGlue()); box.setBackground(Color.WHITE); box.setOpaque(true); box.setPreferredSize(new Dimension(600, 10000)); lblCursor = new JLabel("Cursor"); lblCursor.setBorder(new EtchedBorder()); JPanel boxText = new JPanel(new BorderLayout()); boxText.setBorder(new EmptyBorder(5, 5, 5, 5)); boxText.add(lblDocument, BorderLayout.NORTH); boxText.add(new JScrollPane(box), BorderLayout.CENTER); boxText.add(lblCursor, BorderLayout.SOUTH); JLabel lblPars = new JLabel("Paragraphs: "); lblPars.setBorder(new EtchedBorder()); parList = new JList(); parList.setPreferredSize(new Dimension(100, 300)); parList.setEnabled(false); JPanel boxPars = new JPanel(new BorderLayout()); boxPars.setBorder(new EmptyBorder(5, 5, 5, 5)); boxPars.add(lblPars, BorderLayout.NORTH); boxPars.add(new JScrollPane(parList), BorderLayout.CENTER); add(boxText, BorderLayout.CENTER); add(boxPars, BorderLayout.EAST); }