Java Code Examples for javax.swing.JTextArea#setCaretPosition()

The following examples show how to use javax.swing.JTextArea#setCaretPosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClearVolumeTCPClientHelper.java    From clearvolume with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void showEditableOptionPane(final String pText,
									final String pTitle,
									final int pMessageType)
{
	final JTextArea ta = new JTextArea(48, 100);
	ta.setText(pText);
	ta.setWrapStyleWord(true);
	ta.setLineWrap(false);
	ta.setCaretPosition(0);
	ta.setEditable(false);

	JOptionPane.showMessageDialog(	null,
									new JScrollPane(ta),
									pTitle,
									pMessageType);
}
 
Example 2
Source File: PanelLogging.java    From java-ocr-api with GNU Affero General Public License v3.0 6 votes vote down vote up
private static void logAndScrollToBottom(final JTextArea textArea, final String mesg) {
    if(! SwingUtilities.isEventDispatchThread()) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                logAndScrollToBottom(textArea, mesg);
            }
        });
        return;
    }
    textArea.append(textArea.getText().length() == 0 ? mesg : "\n" + mesg);

    String text = textArea.getText();
    if(text.length() > 2) {
        int lastLinePos = text.lastIndexOf("\n", text.length() - 2);
        if(lastLinePos > 0) {
            textArea.setCaretPosition(lastLinePos + 1); 
        }
    }
}
 
Example 3
Source File: EditorFindSupportTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Test of replaceAll method, of class EditorFindSupport.
 */
@Test
public void testReplaceAll2() throws Exception {
    final Map<String, Object> props = new HashMap<>();
    props.put(EditorFindSupport.FIND_WHAT, "ahoj");
    props.put(EditorFindSupport.FIND_REPLACE_WITH, "xxx");
    props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));

    final EditorFindSupport instance = EditorFindSupport.getInstance();
    JTextArea ta = new JTextArea("0123456789 ahoj ahoj svete");
    ta.setCaretPosition(15);
    instance.replaceAllImpl(props, ta);
    assertEquals("0123456789 ahoj xxx svete", ta.getText());
}
 
Example 4
Source File: EditorFindSupportTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Test of replaceAll method, of class EditorFindSupport.
 */
@Test
public void testReplaceAll7() throws Exception {
    final Map<String, Object> props = new HashMap<>();
    props.put(EditorFindSupport.FIND_WHAT, "ahoj");
    props.put(EditorFindSupport.FIND_REPLACE_WITH, "xxx");
    props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));

    final EditorFindSupport instance = EditorFindSupport.getInstance();
    JTextArea ta = new JTextArea("0123456789 ahojahojahoj svete");
    ta.setCaretPosition(ta.getText().length()-1);
    instance.replaceAllImpl(props, ta);
    assertEquals("0123456789 xxxxxxxxx svete", ta.getText());
}
 
Example 5
Source File: EditorFindSupportTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Test of replaceAll method, of class EditorFindSupport.
 */
@Test
public void testReplaceAll9() throws Exception {
    final Map<String, Object> props = new HashMap<>();
    props.put(EditorFindSupport.FIND_WHAT, "a");
    props.put(EditorFindSupport.FIND_REPLACE_WITH, "b");
    props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));

    final EditorFindSupport instance = EditorFindSupport.getInstance();
    JTextArea ta = new JTextArea("aa");
    ta.setCaretPosition(1);
    instance.replaceAllImpl(props, ta);
    assertEquals("bb", ta.getText());
}
 
Example 6
Source File: MainFrame.java    From procamtracker with GNU General Public License v2.0 5 votes vote down vote up
private void readmeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_readmeMenuItemActionPerformed
    try {
        JTextArea textArea = new JTextArea();
        Font font = textArea.getFont();
        textArea.setFont(new Font("Monospaced", font.getStyle(), font.getSize()));
        textArea.setEditable(false);

        String text = "";
        BufferedReader r = new BufferedReader(new FileReader(
                myDirectory + File.separator + "../README.md"));
        String line;
        while ((line = r.readLine()) != null) {
            text += line + '\n';
        }

        textArea.setText(text);
        textArea.setCaretPosition(0);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setColumns(80);

        // stuff it in a scrollpane with a controlled size.
        JScrollPane scrollPane = new JScrollPane(textArea);
        Dimension dim = textArea.getPreferredSize();
        dim.height = dim.width*50/80;
        scrollPane.setPreferredSize(dim);

        // pass the scrollpane to the joptionpane.
        JDialog dialog = new JOptionPane(scrollPane, JOptionPane.PLAIN_MESSAGE).
                createDialog(this, "README");
        dialog.setResizable(true);
        dialog.setModalityType(ModalityType.MODELESS);
        dialog.setVisible(true);
    } catch (Exception ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example 7
Source File: Utilities.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * Affiche un texte scrollable non éditable dans une popup.
 * @param component Parent
 * @param title Titre de la popup
 * @param text Texte
 */
public static void showTextInPopup(Component component, String title, String text) {
	final JTextArea textArea = new JTextArea();
	textArea.setText(text);
	textArea.setEditable(false);
	textArea.setCaretPosition(0);
	// background nécessaire avec la plupart des look and feels dont Nimbus,
	// sinon il reste blanc malgré editable false
	textArea.setBackground(Color.decode("#E6E6E6"));
	final JScrollPane scrollPane = new JScrollPane(textArea);

	final JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 5));
	final MButton clipBoardButton = new MButton(
			I18NAdapter.getString("Copier_dans_presse-papiers"));
	clipBoardButton.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			textArea.selectAll();
			textArea.copy();
			textArea.setCaretPosition(0);
		}
	});
	buttonPanel.setOpaque(false);
	buttonPanel.add(clipBoardButton);

	final Window window = SwingUtilities.getWindowAncestor(component);
	final JDialog dialog = new JDialog((JFrame) window, title, true);
	final JPanel contentPane = new JPanel(new BorderLayout());
	contentPane.add(scrollPane, BorderLayout.CENTER);
	contentPane.add(buttonPanel, BorderLayout.SOUTH);
	dialog.setContentPane(contentPane);
	dialog.pack();
	dialog.setLocationRelativeTo(window);
	dialog.setVisible(true);
}
 
Example 8
Source File: AboutDialog.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates a panel showing the licence.
 *
 * @return a panel.
 */
private JPanel createLicencePanel() {

  final JPanel licencePanel = new JPanel( new BorderLayout() );
  final JTextArea area = new JTextArea( this.licence );
  area.setLineWrap( true );
  area.setWrapStyleWord( true );
  area.setCaretPosition( 0 );
  area.setEditable( false );
  licencePanel.add( new JScrollPane( area ) );
  return licencePanel;

}
 
Example 9
Source File: LicenseDialog.java    From openvisualtraceroute with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Constructor
 */
public LicenseDialog(final Window parent) {
	super(parent, Resources.getLabel("license.button"), ModalityType.APPLICATION_MODAL);
	getContentPane().add(createHeaderPanel(false, null), BorderLayout.NORTH);
	final JTextArea license = new JTextArea(30, 50);
	license.setEditable(false);
	// read the license file and add its content to the JTextArea
	for (final String line : Util.readUTF8File(Resources.class.getResourceAsStream("/" + Resources.RESOURCE_PATH + "/License.txt"))) {
		license.append("   " + line + "\n");
	}
	// scroll to the top of the JTextArea
	license.setCaretPosition(0);
	// the all thing in a ScrollPane
	final JScrollPane scroll = new JScrollPane(license);
	getContentPane().add(scroll, BorderLayout.CENTER);
	final JPanel donatePanel = new JPanel(new BorderLayout(5, 10));
	final JLabel donate = new JLabel(Resources.getLabel("donate"));
	donatePanel.add(donate, BorderLayout.NORTH);
	final JPanel center = new JPanel();
	center.setLayout(new FlowLayout());
	center.add(new JLabel(Resources.getImageIcon("donate.png")));
	center.add(new HyperlinkLabel(Resources.getLabel("donate.label"), Env.INSTANCE.getDonateUrl()));
	donatePanel.add(center, BorderLayout.CENTER);
	final JButton close = new JButton(Resources.getLabel("close.button"));
	close.addActionListener(e -> LicenseDialog.this.dispose());
	donatePanel.add(close, BorderLayout.SOUTH);
	getContentPane().add(donatePanel, BorderLayout.SOUTH);
	SwingUtilities4.setUp(this);
	getRootPane().registerKeyboardAction(e -> dispose(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
}
 
Example 10
Source File: EditorFindSupportTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Test of replaceAll method, of class EditorFindSupport for regressions in #165497.
 */
@Test
public void testReplaceAllFinishes_165497_e() throws Exception {
    final Map<String, Object> props = new HashMap<>();
    props.put(EditorFindSupport.FIND_WHAT, "a");
    props.put(EditorFindSupport.FIND_REPLACE_WITH, "a");
    props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));

    final EditorFindSupport instance = EditorFindSupport.getInstance();
    final boolean [] finished = new boolean[1];
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            JTextArea ta = new JTextArea("aaaa");
            ta.setCaretPosition(2);
            instance.replaceAllImpl(props, ta);
            finished[0] = true;
        }
    });
    t.start();
    Thread.sleep(2000);
    if (!finished[0]) {
        t.stop();
    }
    assertTrue(finished[0]);
}
 
Example 11
Source File: MainFrame.java    From procamcalib with GNU General Public License v2.0 5 votes vote down vote up
private void readmeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_readmeMenuItemActionPerformed
    try {
        JTextArea textArea = new JTextArea();
        Font font = textArea.getFont();
        textArea.setFont(new Font("Monospaced", font.getStyle(), font.getSize()));
        textArea.setEditable(false);

        String text = "";
        BufferedReader r = new BufferedReader(new FileReader(
                myDirectory + File.separator + "../README.md"));
        String line;
        while ((line = r.readLine()) != null) {
            text += line + '\n';
        }

        textArea.setText(text);
        textArea.setCaretPosition(0);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setColumns(80);

        // stuff it in a scrollpane with a controlled size.
        JScrollPane scrollPane = new JScrollPane(textArea);
        Dimension dim = textArea.getPreferredSize();
        dim.height = dim.width*50/80;
        scrollPane.setPreferredSize(dim);

        // pass the scrollpane to the joptionpane.
        JDialog dialog = new JOptionPane(scrollPane, JOptionPane.PLAIN_MESSAGE).
                createDialog(this, "README");
        dialog.setResizable(true);
        dialog.setModalityType(ModalityType.MODELESS);
        dialog.setVisible(true);
    } catch (Exception ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example 12
Source File: ExceptionDialog.java    From settlers-remake with MIT License 5 votes vote down vote up
/**
 * Prepare the contents
 * 
 * @param e
 *            Exception
 * @param description
 *            Description
 * @param t
 *            Thread
 * @return Panel with all iformation
 */
private JPanel prepareContents(Throwable e, String description, Thread t) {
	JPanel p = new JPanel();
	p.setLayout(new BorderLayout());
	JTextArea txt = new JTextArea(10, 40);

	StringBuilder b = new StringBuilder();

	b.append("```\n");
	b.append(description);
	b.append("\n---\n== Thread ==");
	b.append("\nname: ");
	b.append(t.getName());
	b.append("\nID: ");
	b.append(t.getId());
	b.append("\n---\n== Exception ==");
	b.append("\nclass: ");
	b.append(e.getClass().getName());
	b.append("\nmessage: ");
	b.append(e.getMessage());
	b.append("\nmessage: ");
	b.append(e.getMessage());
	b.append("\nstacktrace:\n");

	StringWriter errors = new StringWriter();
	e.printStackTrace(new PrintWriter(errors));
	b.append(errors.toString());

	b.append("```\n");

	errorString = b.toString();
	txt.setText(b.toString());
	txt.setCaretPosition(0);

	p.add(new JScrollPane(txt), BorderLayout.CENTER);
	return p;
}
 
Example 13
Source File: EditorFindSupportTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceFindFocused() throws Exception {
    final Map<String, Object> props = new HashMap<>();
    props.put(EditorFindSupport.FIND_WHAT, "a");
    props.put(EditorFindSupport.FIND_REPLACE_WITH, "b");
    props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));
    
    final EditorFindSupport instance = EditorFindSupport.getInstance();
    JTextArea ta = new JTextArea("aaaa");
    ta.setCaretPosition(0);
    instance.setFocusedTextComponent(ta);
    instance.replace(props, false);
    instance.find(props, false);
    assertEquals("baaa", ta.getText());
    instance.replace(props, false);
    instance.find(props, false);
    assertEquals("bbaa", ta.getText());
    instance.replace(props, false);
    instance.find(props, false);
    assertEquals("bbba", ta.getText());
}
 
Example 14
Source File: EditorFindSupportTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceFind() throws Exception {
    final Map<String, Object> props = new HashMap<>();
    props.put(EditorFindSupport.FIND_WHAT, "a");
    props.put(EditorFindSupport.FIND_REPLACE_WITH, "b");
    props.put(EditorFindSupport.FIND_HIGHLIGHT_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_INC_SEARCH, Boolean.TRUE);
    props.put(EditorFindSupport.FIND_BACKWARD_SEARCH, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_WRAP_SEARCH, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_MATCH_CASE, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_SMART_CASE, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_REG_EXP, Boolean.FALSE);
    props.put(EditorFindSupport.FIND_HISTORY, new Integer(30));
    
    final EditorFindSupport instance = EditorFindSupport.getInstance();
    JTextArea ta = new JTextArea("aaaa");
    ta.setCaretPosition(0);
    instance.replaceImpl(props, false, ta);
    instance.findReplaceImpl(null, props, false, ta);
    assertEquals("baaa", ta.getText());
    instance.replaceImpl(props, false, ta);
    instance.findReplaceImpl(null, props, false, ta);
    assertEquals("bbaa", ta.getText());
    instance.replaceImpl(props, false, ta);
    instance.findReplaceImpl(null, props, false, ta);
    assertEquals("bbba", ta.getText());
}
 
Example 15
Source File: DViewPEM.java    From portecle with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Initialize the dialog's GUI components.
 *
 * @throws CryptoException A problem was encountered getting the object's PEM encoding
 */
private void initComponents()
    throws CryptoException
{
	if (m_pem == null)
	{
		StringWriter encoded = new StringWriter();
		try (JcaPEMWriter pw = new JcaPEMWriter(encoded))
		{
			pw.writeObject(m_object);
		}
		catch (IOException e)
		{
			throw new CryptoException(RB.getString("DViewPEM.exception.message"), e);
		}
		m_pem = encoded.toString();
	}

	JPanel jpButtons = new JPanel(new FlowLayout(FlowLayout.CENTER));

	JButton jbOK = getOkButton(true);

	final JButton jbSave = new JButton(RB.getString("DViewPEM.jbSave.text"));
	jbSave.setMnemonic(RB.getString("DViewPEM.jbSave.mnemonic").charAt(0));
	if (m_chooser == null || m_pem == null)
	{
		jbSave.setEnabled(false);
	}
	else
	{
		jbSave.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent evt)
			{
				savePressed();
			}
		});
	}

	jpButtons.add(jbOK);
	jpButtons.add(jbSave);

	JPanel jpPEM = new JPanel(new BorderLayout());
	jpPEM.setBorder(new EmptyBorder(5, 5, 5, 5));

	// Load text area with the PEM encoding
	JTextArea jtaPEM = new JTextArea(m_pem);
	jtaPEM.setCaretPosition(0);
	jtaPEM.setEditable(false);
	jtaPEM.setFont(new Font(Font.MONOSPACED, Font.PLAIN, jtaPEM.getFont().getSize()));

	JScrollPane jspPEM = new JScrollPane(jtaPEM, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
	    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
	jspPEM.setPreferredSize(new Dimension(500, 300));
	jpPEM.add(jspPEM, BorderLayout.CENTER);

	getContentPane().add(jpPEM, BorderLayout.CENTER);
	getContentPane().add(jpButtons, BorderLayout.SOUTH);

	getRootPane().setDefaultButton(jbOK);

	initDialog();

	setResizable(true);
	jbOK.requestFocusInWindow();
}
 
Example 16
Source File: Main.java    From SIMVA-SoS with Apache License 2.0 4 votes vote down vote up
public static void consolePrint(JTextArea console, String text) {
    console.append(text);
    console.setCaretPosition(console.getDocument().getLength());
    console.requestFocus();
}
 
Example 17
Source File: EULADialog.java    From azure-devops-intellij with MIT License 4 votes vote down vote up
private EULADialog(@Nullable Project project, @NotNull String title, @Nullable String eulaText, boolean isPlainText, OnEulaAccepted onAccept) {
    super(project);
    this.onAccept = onAccept;
    myEulaTextFound = eulaText != null;

    JComponent component;
    if (isPlainText) {
        JTextArea textArea = new JTextArea();
        textArea.setText(eulaText);
        textArea.setMinimumSize(null);
        textArea.setEditable(false);
        textArea.setCaretPosition(0);
        textArea.setBackground(null);
        textArea.setBorder(JBUI.Borders.empty());

        component = textArea;
    } else {
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.getDocument().putProperty("IgnoreCharsetDirective", Boolean.TRUE); // work around <meta> tag
        editor.setText(eulaText);
        editor.setEditable(false);
        editor.setCaretPosition(0);
        editor.setMinimumSize(null);
        editor.setBorder(JBUI.Borders.empty());

        component = editor;
    }

    myScrollPane = new JBScrollPane(component);
    myScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    setModal(true);
    setTitle(title);

    setOKButtonText("Accept");
    getOKAction().putValue(DEFAULT_ACTION, null);

    setCancelButtonText("Decline");
    getCancelAction().putValue(DEFAULT_ACTION, Boolean.TRUE);

    init();
}
 
Example 18
Source File: GeneralPropertiesProvider.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
private static void updateProperties(JTextArea textArea, String hostname, String ip) {
    String dnsName = NbBundle.getMessage(GeneralPropertiesProvider.class, "LBL_DnsName"); // NOI18N
    String ipAddress = NbBundle.getMessage(GeneralPropertiesProvider.class, "LBL_IpAddress"); // NOI18N
    textArea.setText(dnsName + " " + hostname + "\n" + ipAddress + " " + ip); // NOI18N
    textArea.setCaretPosition(0);
}
 
Example 19
Source File: ToStringGeneratorTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testToStringExists() throws Exception {
    FileObject javaFile = FileUtil.createData(fo, "NewClass.java");
    String what1 = ""
            + "public class NewClass {\n"
            + "    private final String test1 = \"test\";\n"
            + "    private final String test2 = \"test\";\n"
            + "    private final String test3 = \"test\";\n";

    String what2 = ""
            + "\n"
            + "    @Override\n"
            + "    public String toString() {\n"
            + "        StringBuilder sb = new StringBuilder();\n"
            + "        sb.append(\"NewClass{\");\n"
            + "        sb.append(\"test1=\").append(test1);\n"
            + "        sb.append(\", \");\n"
            + "        sb.append(\"test2=\").append(test2);\n"
            + "        sb.append(\", \");\n"
            + "        sb.append(\"test3=\").append(test3);\n"
            + "        sb.append('}');\n"
            + "        return sb.toString();\n"
            + "    }\n"
            + "\n"
            + "}";
    String what = what1 + what2;
    GeneratorUtilsTest.writeIntoFile(javaFile, what);

    JavaSource javaSource = JavaSource.forFileObject(javaFile);
    assertNotNull("Created", javaSource);

    Document doc = getDocuemnt(javaFile);

    final JTextArea component = new JTextArea(doc);
    component.setCaretPosition(what1.length());

    class Task implements org.netbeans.api.java.source.Task<CompilationController> {

        private ToStringGenerator generator;

        @Override
        public void run(CompilationController controller) throws Exception {
            controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
            Element typeElement = controller.getElements().getTypeElement("NewClass");
            generator = ToStringGenerator.createToStringGenerator(component, controller, typeElement, true);
        }

        public void post() throws Exception {
            assertNull("Not created", generator);
        }
    }
    final Task task = new Task();

    javaSource.runUserActionTask(task, false);
    task.post();
}
 
Example 20
Source File: ToStringGeneratorTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testToStringWithPlusOperator() throws Exception {
    FileObject javaFile = FileUtil.createData(fo, "NewClass.java");
    String what1 = ""
            + "public class NewClass {\n"
            + "    private final String test1 = \"test\";\n"
            + "    private final String test2 = \"test\";\n"
            + "    private final String test3 = \"test\";\n";

    String what2 = ""
            + "\n"
            + "}";
    String what = what1 + what2;
    GeneratorUtilsTest.writeIntoFile(javaFile, what);

    JavaSource javaSource = JavaSource.forFileObject(javaFile);
    assertNotNull("Created", javaSource);

    Document doc = getDocuemnt(javaFile);

    final JTextArea component = new JTextArea(doc);
    component.setCaretPosition(what1.length());

    class Task implements org.netbeans.api.java.source.Task<CompilationController> {

        private ToStringGenerator generator;

        @Override
        public void run(CompilationController controller) throws Exception {
            controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
            Element typeElement = controller.getElements().getTypeElement("NewClass");
            generator = ToStringGenerator.createToStringGenerator(component, controller, typeElement, false);
        }

        public void post() throws Exception {
            assertNotNull("Created", generator);

            assertEquals("Three fields", 3, generator.getDescription().getSubs().size());
            assertEquals("test1 field selected", true, generator.getDescription().getSubs().get(0).isSelected());
            assertEquals("test2 field selected", true, generator.getDescription().getSubs().get(1).isSelected());
            assertEquals("test3 field selected", true, generator.getDescription().getSubs().get(2).isSelected());
            assertEquals("Don't use StringBuilder", false, generator.useStringBuilder());
        }
    }
    final Task task = new Task();

    javaSource.runUserActionTask(task, false);
    task.post();

    SwingUtilities.invokeAndWait(() -> task.generator.invoke());

    Document document = component.getDocument();
    String text = document.getText(0, document.getLength());
    String expected = ""
            + "public class NewClass {\n"
            + "    private final String test1 = \"test\";\n"
            + "    private final String test2 = \"test\";\n"
            + "    private final String test3 = \"test\";\n"
            + "\n"
            + "    @Override\n"
            + "    public String toString() {\n"
            + "        return \"NewClass{\" + \"test1=\" + test1 + \", test2=\" + test2 + \", test3=\" + test3 + '}';\n"
            + "    }\n"
            + "\n"
            + "}";
    assertEquals(expected, text);
}