List of usage examples for javax.swing.text JTextComponent getText
public String getText()
TextComponent
. From source file:org.company.processmaker.TreeFilesTopComponent.java
public TreeFilesTopComponent() { initComponents();//w w w. j a va 2s . c om setName(Bundle.CTL_TreeFilesTopComponent()); setToolTipText(Bundle.HINT_TreeFilesTopComponent()); // new codes instance = this; MouseListener ml = new MouseAdapter() { public void mousePressed(MouseEvent e) { int selRow = jTree1.getRowForLocation(e.getX(), e.getY()); TreePath selPath = jTree1.getPathForLocation(e.getX(), e.getY()); if (selRow != -1) { if (e.getClickCount() == 1) { //mySingleClick(selRow, selPath); } else if (e.getClickCount() == 2) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1 .getLastSelectedPathComponent(); if (node == null) { return; } Object nodeInfo = node.getUserObject(); int node_level = node.getLevel(); if (node_level < 2) { return; } // for each dyna form if (node_level == 2) { Global gl_obj = Global.getInstance(); //myDoubleClick(selRow, selPath); conf = Config.getInstance(); DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent(); String parentName = (String) parent.getUserObject(); // handle triggers if (parentName.equals("Triggers")) { String filePath = ""; for (String[] s : res_trigger) { if (s[0].equals(nodeInfo.toString())) { // get path of dyna in xml forms filePath = conf.tmp + "triggers/" + s[3] + "/" + s[2] + ".php"; break; } } File toAdd = new File(filePath); try { DataObject dObject = DataObject.find(FileUtil.toFileObject(toAdd)); dObject.getLookup().lookup(OpenCookie.class).open(); // dont listen for exist listen files if (existFile(filePath)) { return; } dObject.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { if (DataObject.PROP_MODIFIED.equals(evt.getPropertyName())) { //fire a dummy event if (!Boolean.TRUE.equals(evt.getNewValue())) { /*String msg = "Saved to" + evt.toString(); NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd);*/ TopComponent activeTC = TopComponent.getRegistry() .getActivated(); DataObject dataLookup = activeTC.getLookup() .lookup(DataObject.class); String filePath = FileUtil.toFile(dataLookup.getPrimaryFile()) .getAbsolutePath(); File userFile = new File(filePath); String fileName = userFile.getName(); fileName = fileName.substring(0, fileName.lastIndexOf(".")); try { String content = new String( Files.readAllBytes(Paths.get(filePath))); // remote php tag and info "<?php //don't remove this tag! \n" content = content.substring(6, content.length()); String query = "update triggers set TRI_WEBBOT = '" + StringEscapeUtils.escapeSql(content) + "' where TRI_UID = '" + fileName + "'"; GooglePanel.updateQuery(query); } catch (Exception e) { //Exceptions.printStackTrace(e); String msg = "Can not update trigger"; NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd); } } } } }); } catch (DataObjectNotFoundException ex) { //Exceptions.printStackTrace(ex); String msg = "Trigger not found"; NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd); } return; } List<String[]> res_dyna = gl_obj.getDyna(); String FileDir = ""; for (String[] s : res_dyna) { if (s[1].equals(nodeInfo.toString())) { // get path of dyna in xml forms FileDir = s[3]; break; } } //String msg = "selRow" + nodeInfo.toString() + "|" + conf.getXmlForms() + FileDir; String filePath = conf.getXmlForms() + FileDir + ".xml"; if (conf.isRemote()) { String[] res = FileDir.split("/"); filePath = conf.get("local_tmp_for_remote") + "/" + FileDir + "/" + res[1] + ".xml"; } File toAdd = new File(filePath); //Result will be null if the user clicked cancel or closed the dialog w/o OK if (toAdd != null) { try { DataObject dObject = DataObject.find(FileUtil.toFileObject(toAdd)); dObject.getLookup().lookup(OpenCookie.class).open(); // dont listen for exist listen files if (existFile(filePath)) { return; } dObject.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { if (DataObject.PROP_MODIFIED.equals(evt.getPropertyName())) { //fire a dummy event if (!Boolean.TRUE.equals(evt.getNewValue())) { /*String msg = "Saved to" + evt.toString(); NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd);*/ TopComponent activeTC = TopComponent.getRegistry() .getActivated(); DataObject dataLookup = activeTC.getLookup() .lookup(DataObject.class); String filePath = FileUtil.toFile(dataLookup.getPrimaryFile()) .getAbsolutePath(); File userFile = new File(filePath); String fileName = userFile.getName(); fileName = fileName.substring(0, fileName.lastIndexOf(".")); Global gl_obj = Global.getInstance(); List<String[]> res_dyna = gl_obj.getDyna(); String FileDir = ""; for (String[] s : res_dyna) { if (filePath.contains(s[0])) { FileDir = s[3]; break; } } if (conf.isRemote()) { boolean res_Upload = SSH.getInstance().uplaodFile(FileDir); if (res_Upload) { String msg = "file upload Successfully!"; NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd); } else { String msg = "error in uploading file!"; NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd); } } } } } }); } catch (DataObjectNotFoundException ex) { //Exceptions.printStackTrace(ex); String msg = "Can not find xml file"; NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd); } } } // for each js file if (node_level == 3) { TreeNode parentInfo = node.getParent(); Global gl_obj = Global.getInstance(); List<String[]> res_dyna = gl_obj.getDyna(); String FileDir = ""; for (String[] s : res_dyna) { if (s[1].equals(parentInfo.toString())) { // get path of dyna in xml forms FileDir = s[3]; break; } } //myDoubleClick(selRow, selPath); conf = Config.getInstance(); String filePath = conf.tmp + "xmlForms/" + FileDir + "/" + nodeInfo.toString() + ".js"; if (conf.isRemote()) { filePath = conf.get("local_tmp_for_remote") + FileDir + "/" + nodeInfo.toString() + ".js"; } File toAdd = new File(filePath); //Result will be null if the user clicked cancel or closed the dialog w/o OK if (toAdd != null) { try { DataObject dObject = DataObject.find(FileUtil.toFileObject(toAdd)); dObject.getLookup().lookup(OpenCookie.class).open(); // dont listen for exist listen files if (existFile(filePath)) { return; } dObject.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { if (DataObject.PROP_MODIFIED.equals(evt.getPropertyName())) { //fire a dummy event if (!Boolean.TRUE.equals(evt.getNewValue())) { JTextComponent ed = EditorRegistry.lastFocusedComponent(); String jsDoc = ""; try { jsDoc = ed.getText(); } catch (Exception ex) { //Exceptions.printStackTrace(ex); String msg = "Can not get text from editor"; NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd); } TopComponent activeTC = TopComponent.getRegistry() .getActivated(); DataObject dataLookup = activeTC.getLookup() .lookup(DataObject.class); String filePath = FileUtil.toFile(dataLookup.getPrimaryFile()) .getAbsolutePath(); File userFile = new File(filePath); String fileName = userFile.getName(); fileName = fileName.substring(0, fileName.lastIndexOf(".")); Global gl_obj = Global.getInstance(); List<String[]> res_dyna = gl_obj.getDyna(); String FileDir = ""; for (String[] s : res_dyna) { if (filePath.contains(s[0])) { FileDir = s[3]; break; } } String fullPath = conf.getXmlForms() + FileDir + ".xml"; if (conf.isRemote()) { String[] res = FileDir.split("/"); fullPath = conf.get("local_tmp_for_remote") + FileDir + "/" + res[1] + ".xml"; } try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document mainDoc = builder.parse(fullPath); XPath xPath = XPathFactory.newInstance().newXPath(); Node startDateNode = (Node) xPath .compile("//dynaForm/" + fileName) .evaluate(mainDoc, XPathConstants.NODE); Node cdata = mainDoc.createCDATASection(jsDoc); startDateNode.setTextContent(""); startDateNode.appendChild(cdata); /*String msg = evt.getPropertyName() + "-" + fileName; NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd);*/ // write the content into xml file TransformerFactory transformerFactory = TransformerFactory .newInstance(); Transformer transformer = transformerFactory .newTransformer(); DOMSource source = new DOMSource(mainDoc); StreamResult result = new StreamResult(new File(fullPath)); transformer.transform(source, result); if (conf.isRemote()) { boolean res_Upload = SSH.getInstance() .uplaodFile(FileDir); if (res_Upload) { String msg = "file upload Successfully!"; NotifyDescriptor nd = new NotifyDescriptor.Message( msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd); } else { String msg = "error in uploading file!"; NotifyDescriptor nd = new NotifyDescriptor.Message( msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd); } } } catch (Exception ex) { //Exceptions.printStackTrace(ex); String msg = "Can not save to xml form"; NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd); } } } } }); } catch (DataObjectNotFoundException ex) { //Exceptions.printStackTrace(ex); String msg = "Can not save to xml form"; NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE); DialogDisplayer.getDefault().notify(nd); } } } } } } }; jTree1.addMouseListener(ml); jTree1.setModel(null); }
From source file:org.eclipse.jubula.rc.swing.listener.RecordActions.java
/** * creates CAP for Actions Replace Text//from w w w . j a v a 2 s . com * @param source Component */ protected void replaceText(Component source) { Component src = source; Component parent = getComponentParent() != null ? getComponentParent() : src.getParent(); if (parent instanceof JComboBox) { src = parent; } String text = null; boolean isEditable = false; boolean isCbxItem = false; boolean isSupported = true; if (src instanceof JTextComponent) { JTextComponent jtf = (JTextComponent) src; text = jtf.getText(); isEditable = jtf.isEditable(); if ((source instanceof JTextArea || source instanceof JTextPane || source instanceof JEditorPane) && (text.indexOf(CharacterConstants.LINEFEED) != -1 || text.indexOf(CharacterConstants.RETURN) != -1)) { isSupported = false; sendInfoMessage(Constants.REC_MULTILINE_MSG); } if (parent instanceof JTable) { JTable tbl = (JTable) parent; replaceTableText(src, tbl, text); return; } } if (src instanceof JComboBox) { JComboBox cbx = (JComboBox) src; isEditable = cbx.isEditable(); if (isEditable) { ComboBoxEditor cbxEditor = cbx.getEditor(); text = cbxEditor.getItem().toString(); String[] cbxItems = m_recordHelper.getRenderedComboItems(cbx); for (int i = 0; i < cbxItems.length; i++) { String item = cbxItems[i]; if (item.equals(text)) { isCbxItem = true; } } } else { return; } } if (text.length() > Constants.REC_MAX_STRING_LENGTH) { ShowObservInfoMessage infoMsg = new ShowObservInfoMessage(Constants.REC_MAX_STRING_MSG); try { AUTServer.getInstance().getServerCommunicator().send(infoMsg); } catch (CommunicationException e) { // no log available here } return; } if (m_map.get(source) != null && !(text.equals(m_map.get(source).toString())) && isSupported && isEditable && !isCbxItem) { m_map.put(src, text); IComponentIdentifier id = null; try { id = ComponentHandler.getIdentifier(src); Action a = new Action(); a = m_recordHelper.compSysToAction(id, "CompSystem.InputText"); //$NON-NLS-1$ List parameterValues = new LinkedList(); text = StringParsing.singleQuoteText(text); parameterValues.add(text); String logName = createLogicalName(src, id); createCAP(a, id, parameterValues, logName); } catch (NoIdentifierForComponentException nifce) { // no identifier for the component, log this as an error log.error("no identifier for '" + src); //$NON-NLS-1$ } } }
From source file:org.executequery.gui.text.TextUtilities.java
public static int save(JTextComponent textComponent) { TextFileWriter writer = new TextFileWriter(textComponent.getText()); int result = writer.write(); writer = null;/*from ww w. j a v a2 s . co m*/ return result; }
From source file:org.executequery.gui.text.TextUtilities.java
public static void deleteLine(JTextComponent textComponent) { char newLine = '\n'; String _newLine = "\n"; int caretIndex = textComponent.getCaretPosition(); String text = textComponent.getText(); StringBuilder sb = new StringBuilder(text); int endOfLineIndexBefore = -1; int endOfLineIndexAfter = sb.indexOf(_newLine, caretIndex); char[] textChars = text.toCharArray(); for (int i = 0; i < textChars.length; i++) { if (i >= caretIndex) { break; } else {// w ww . j a va 2s .c om if (textChars[i] == newLine) endOfLineIndexBefore = i; } } if (endOfLineIndexBefore == -1) { endOfLineIndexBefore = 0; } if (endOfLineIndexAfter == -1) { sb.delete(endOfLineIndexBefore, sb.length()); } else if (endOfLineIndexBefore == -1) { sb.delete(0, endOfLineIndexAfter + 1); } else if (endOfLineIndexBefore == 0 && endOfLineIndexAfter == 0) { sb.deleteCharAt(0); } else { sb.delete(endOfLineIndexBefore, endOfLineIndexAfter); } textComponent.setText(sb.toString()); if (endOfLineIndexBefore + 1 > sb.length()) { textComponent.setCaretPosition(endOfLineIndexBefore == -1 ? 0 : endOfLineIndexBefore); } else { textComponent.setCaretPosition(endOfLineIndexBefore + 1); } }
From source file:org.executequery.gui.text.TextUtilities.java
public static void deleteWord(JTextComponent textComponent) { char space = ' '; String _space = " "; int caretIndex = textComponent.getCaretPosition(); String text = textComponent.getText(); StringBuilder sb = new StringBuilder(text); int startOfWordIndex = -1; int endOfWordIndex = sb.indexOf(_space, caretIndex); char[] textChars = text.toCharArray(); for (int i = 0; i < textChars.length; i++) { if (i >= caretIndex) { break; }/*from w ww .j a v a2s. c o m*/ else { if (textChars[i] == space) startOfWordIndex = i; } } if (endOfWordIndex == -1) return; else if (startOfWordIndex == 0 && endOfWordIndex == 0) return; else if (startOfWordIndex == endOfWordIndex) return; sb.delete(startOfWordIndex + 1, endOfWordIndex); textComponent.setText(sb.toString()); textComponent.setCaretPosition(startOfWordIndex + 1); }
From source file:org.executequery.gui.text.TextUtilities.java
public static void insertFromFile(JTextComponent textComponent) { StringBuffer buf = null;//from w ww . j ava 2s .c om String text = null; FileChooserDialog fileChooser = new FileChooserDialog(); fileChooser.setDialogTitle("Insert from file"); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setDialogType(JFileChooser.OPEN_DIALOG); int result = fileChooser.showDialog(GUIUtilities.getInFocusDialogOrWindow(), "Insert"); if (result == JFileChooser.CANCEL_OPTION) return; File file = fileChooser.getSelectedFile(); try { FileInputStream input = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); buf = new StringBuffer(10000); char newLine = '\n'; while ((text = reader.readLine()) != null) buf.append(text).append(newLine); reader.close(); reader = null; input.close(); input = null; int index = textComponent.getCaretPosition(); StringBuffer sb = new StringBuffer(textComponent.getText()); sb.insert(index, buf.toString()); textComponent.setText(sb.toString()); textComponent.setCaretPosition(index + buf.length()); } catch (OutOfMemoryError e) { buf = null; text = null; System.gc(); GUIUtilities.displayErrorMessage("Out of Memory.\nThe file is " + "too large to\nopen for viewing."); } catch (IOException e) { e.printStackTrace(); StringBuffer sb = new StringBuffer(); sb.append("An error occurred opening the selected file.").append("\n\nThe system returned:\n") .append(e.getMessage()); GUIUtilities.displayExceptionErrorDialog(sb.toString(), e); } }
From source file:org.executequery.gui.text.TextUtilities.java
public static void insertLineAfter(JTextComponent textComponent) { String newLine = "\n"; int caretIndex = textComponent.getCaretPosition(); StringBuilder sb = new StringBuilder(textComponent.getText()); int endOfLineIndex = sb.indexOf(newLine, caretIndex); int length = sb.length(); if (caretIndex == length || endOfLineIndex == length) sb.append(newLine);/*from w w w . j a v a 2 s .co m*/ else sb.insert(endOfLineIndex == -1 ? 0 : endOfLineIndex, newLine); textComponent.setText(sb.toString()); textComponent.setCaretPosition(endOfLineIndex == -1 ? length : endOfLineIndex + 1); sb = null; }
From source file:org.executequery.gui.text.TextUtilities.java
public static void insertLineBefore(JTextComponent textComponent) { int caretIndex = textComponent.getCaretPosition(); int insertIndex = -1; char newLine = '\n'; String text = textComponent.getText(); char[] textChars = text.toCharArray(); for (int i = 0; i < textChars.length; i++) { if (i > caretIndex) { break; }//from w w w . j a v a2s . co m else { if (textChars[i] == newLine) insertIndex = i; } } StringBuilder sb = new StringBuilder(text); sb.insert(insertIndex == -1 ? 0 : insertIndex, newLine); textComponent.setText(sb.toString()); textComponent.setCaretPosition(insertIndex + 1); }
From source file:org.nuclos.client.wizard.util.NuclosWizardUtils.java
public static FocusAdapter createWizardFocusAdapter() { return new FocusAdapter() { @Override//from w w w.j a v a 2 s. c om public void focusGained(FocusEvent e) { if (e.getSource() instanceof JTextComponent) { JTextComponent tf = (JTextComponent) e.getSource(); tf.setSelectionStart(0); tf.setSelectionEnd(tf.getText().length()); } } }; }
From source file:org.parosproxy.paros.view.FindDialog.java
private void find() { JTextComponent txtComp = lastInvoker; if (txtComp == null) { JFrame parent = (JFrame) (this.getParent()); Component c = parent.getMostRecentFocusOwner(); if (c instanceof JTextComponent) { txtComp = (JTextComponent) c; }/*from w w w . j av a 2s.co m*/ } // ZAP: Check if a JTextComponent was really found. if (txtComp == null) { return; } try { String findText = txtFind.getText().toLowerCase(); String txt = txtComp.getText().toLowerCase(); int startPos = txt.indexOf(findText, txtComp.getCaretPosition()); // Enable Wrap Search if (startPos <= 0) { txtComp.setCaretPosition(0); startPos = txt.indexOf(findText, txtComp.getCaretPosition()); } int length = findText.length(); if (startPos > -1) { txtComp.select(startPos, startPos + length); txtComp.requestFocusInWindow(); txtFind.requestFocusInWindow(); } else { Toolkit.getDefaultToolkit().beep(); } } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } }