private void setText(JTextComponent comp, String text) { if (text == null) { comp.setText(""); comp.setEnabled(false); comp.setEditable(false); } else { comp.setText(text); comp.setEnabled(true); comp.setEditable(true); } comp.setCaretPosition(0); }
public void setEditable(boolean editable) { if (dataComponent instanceof JTextComponent) { ((JTextComponent) dataComponent).setEditable(editable); } else if (dataComponent instanceof JScrollPane) { Component subComponent = ((JScrollPane) dataComponent).getViewport().getView(); if (subComponent instanceof JTextComponent) { ((JTextComponent) subComponent).setEditable(editable); } } else if (dataComponent instanceof JXDatePicker) { ((JXDatePicker) dataComponent).setEditable(editable); } else { dataComponent.setEnabled(editable); } }
protected void makeElementsNotEditable() { final ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(final ActionEvent e) { if (e.getSource() instanceof JCheckBox) { final JCheckBox checkbox = (JCheckBox) e.getSource(); checkbox.setSelected(!checkbox.isSelected()); } } }; for (final String key : new String[] {"ctnBody"}) { final Object component = result.get(key); if ((component != null)) { if (component instanceof JCheckBox) { ((JCheckBox) component).addActionListener(actionListener); } else if (component instanceof JToggleButton) { ((JToggleButton) component).setEnabled(false); } else if (component instanceof JButton) { ((JButton) component).setEnabled(false); } else if (component instanceof JTextComponent) { ((JTextComponent) component).setEditable(false); } } } }
public void testInsertTextChecksThatTheComponentIsEditable() throws Exception { textBox.setText("text"); jTextComponent.setEditable(false); try { textBox.insertText("new text", 0); throw new AssertionFailureNotDetectedError(); } catch (AssertionFailedError e) { assertEquals("The text box is not editable", e.getMessage()); } assertEquals("text", jTextComponent.getText()); }
public void testInsertText() throws Exception { jTextComponent.setEditable(true); textBox.insertText("text", 0); assertEquals("text", textBox.getText()); textBox.insertText("this is some ", 0); assertEquals("this is some text", textBox.getText()); textBox.insertText("interesting ", 13); assertEquals("this is some interesting text", textBox.getText()); textBox.insertText(", isn't it?", textBox.getText().length()); assertEquals("this is some interesting text, isn't it?", textBox.getText()); }
/** Build the interface. */ private void buildUI() { if (colorEnabled) { JTextPane text = new JTextPane(); this.textComponent = text; } else { JTextArea text = new JTextArea(); this.textComponent = text; text.setLineWrap(true); } textComponent.addMouseListener(this); textComponent.setFont(getMonospaceFont()); textComponent.setEditable(false); DefaultCaret caret = (DefaultCaret) textComponent.getCaret(); caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE); document = textComponent.getDocument(); document.addDocumentListener(new LimitLinesDocumentListener(numLines, true)); JScrollPane scrollText = new JScrollPane(textComponent); scrollText.setBorder(null); scrollText.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); add(scrollText, BorderLayout.CENTER); }
private void addJTextComponent(JTextComponent component, String value, Rectangle rec) { component.setText(value); component.setEditable(false); component.setBounds(rec); add(component); }
/* * Use the text component specified as a simply console to display * text messages. * * The messages can either be appended to the end of the console or * inserted as the first line of the console. */ public MessageConsole(final JTextComponent textComponent, final boolean isAppend) { this.textComponent = textComponent; this.document = textComponent.getDocument(); this.isAppend = isAppend; textComponent.setEditable(false); }
/** * Add a new entry to this query that represents the given attribute. The name of the entry will * be set to the name of the attribute, and the attribute will be attached to the entry, so that * if the attribute is updated, then the entry is updated. If the attribute contains an instance * of ParameterEditorStyle, then defer to the style to create the entry, otherwise just create a * default entry. The style used in a default entry depends on the class of the attribute and on * its declared type, but defaults to a one-line entry if there is no obviously better style. Only * the first style that is found is used to create an entry. * * @param attribute The attribute to create an entry for. */ public void addStyledEntry(Settable attribute) { // Note: it would be nice to give // multiple styles to specify to create more than one // entry for a particular parameter. However, the style configurer // doesn't support it and we don't have a good way of representing // it in this class. // Look for a ParameterEditorStyle. boolean foundStyle = false; try { _addingStyledEntryFor = attribute; if (attribute instanceof NamedObj) { Iterator<?> styles = ((NamedObj) attribute).attributeList(ParameterEditorStyle.class).iterator(); while (styles.hasNext() && !foundStyle) { ParameterEditorStyle style = (ParameterEditorStyle) styles.next(); try { style.addEntry(this); foundStyle = true; } catch (IllegalActionException ex) { // Ignore failures here, and just present // the default dialog. } } } if (!foundStyle) { // NOTE: Infer the style. // This is a regrettable approach, but it keeps // dependence on UI issues out of actor definitions. // Also, the style code is duplicated here and in the // style attributes. However, it won't work to create // a style attribute here, because we don't necessarily // have write access to the workspace. String name = attribute.getName(); String displayName = attribute.getDisplayName(); try { JComponent component = null; if (attribute instanceof IntRangeParameter) { int current = ((IntRangeParameter) attribute).getCurrentValue(); int min = ((IntRangeParameter) attribute).getMinValue(); int max = ((IntRangeParameter) attribute).getMaxValue(); component = addSlider(name, displayName, current, min, max); attachParameter(attribute, name); foundStyle = true; } else if (attribute instanceof DoubleRangeParameter) { double current = ((DoubleToken) ((DoubleRangeParameter) attribute).getToken()).doubleValue(); double max = ((DoubleToken) ((DoubleRangeParameter) attribute).max.getToken()).doubleValue(); double min = ((DoubleToken) ((DoubleRangeParameter) attribute).min.getToken()).doubleValue(); int precision = ((IntToken) ((DoubleRangeParameter) attribute).precision.getToken()).intValue(); // Get the quantized integer for the current value. int quantized = ((int) Math.round(((current - min) * precision) / (max - min))); component = addSlider(name, displayName, quantized, 0, precision); attachParameter(attribute, name); foundStyle = true; } else if (attribute instanceof ColorAttribute) { component = addColorChooser(name, displayName, attribute.getExpression()); attachParameter(attribute, name); foundStyle = true; } else if (attribute instanceof CustomQueryBoxParameter) { JLabel label = new JLabel(displayName + ": "); label.setBackground(_background); component = ((CustomQueryBoxParameter) attribute).createQueryBox(this, attribute); _addPair(name, label, component, component); attachParameter(attribute, name); foundStyle = true; } else if (attribute instanceof FileParameter || attribute instanceof FilePortParameter) { // Specify the directory in which to start browsing // to be the location where the model is defined, // if that is known. URI modelURI = URIAttribute.getModelURI((NamedObj) attribute); File directory = null; if (modelURI != null) { if (modelURI.getScheme().equals("file")) { File modelFile = new File(modelURI); directory = modelFile.getParentFile(); } } URI base = null; if (directory != null) { base = directory.toURI(); } // Check to see whether the attribute being configured // specifies whether files or directories should be listed. // By default, only files are selectable. boolean allowFiles = true; boolean allowDirectories = false; // attribute is always a NamedObj Parameter marker = (Parameter) ((NamedObj) attribute).getAttribute("allowFiles", Parameter.class); if (marker != null) { Token value = marker.getToken(); if (value instanceof BooleanToken) { allowFiles = ((BooleanToken) value).booleanValue(); } } marker = (Parameter) ((NamedObj) attribute).getAttribute("allowDirectories", Parameter.class); if (marker != null) { Token value = marker.getToken(); if (value instanceof BooleanToken) { allowDirectories = ((BooleanToken) value).booleanValue(); } } // FIXME: What to do when neither files nor directories are allowed? if (!allowFiles && !allowDirectories) { // The given attribute will not have a query in the dialog. return; } boolean isOutput = false; if (attribute instanceof FileParameter && ((FileParameter) attribute).isOutput()) { isOutput = true; } // FIXME: Should remember previous browse location? // Next to last argument is the starting directory. component = addFileChooser( name, displayName, attribute.getExpression(), base, directory, allowFiles, allowDirectories, isOutput, preferredBackgroundColor(attribute), preferredForegroundColor(attribute)); attachParameter(attribute, name); foundStyle = true; } else if (attribute instanceof PasswordAttribute) { component = addPassword(name, displayName, ""); attachParameter(attribute, name); foundStyle = true; } else if (attribute instanceof Parameter && (((Parameter) attribute).getChoices() != null)) { Parameter castAttribute = (Parameter) attribute; // NOTE: Make this always editable since Parameter // supports a form of expressions for value propagation. component = addChoice( name, displayName, castAttribute.getChoices(), castAttribute.getExpression(), true, preferredBackgroundColor(attribute), preferredForegroundColor(attribute)); attachParameter(attribute, name); foundStyle = true; } else if ((attribute instanceof NamedObj) && ((((NamedObj) attribute).getAttribute("_textWidthHint") != null) || ((NamedObj) attribute).getAttribute("_textHeightHint") != null)) { // Support hints for text height and/or width so that actors // don't have to use a ParameterEditorStyle, which depends // on packages that depend on graphics. // Default values: int widthValue = 30; int heightValue = 10; Attribute widthAttribute = ((NamedObj) attribute).getAttribute("_textWidthHint"); if (widthAttribute instanceof Variable) { Token token = ((Variable) widthAttribute).getToken(); if (token instanceof IntToken) { widthValue = ((IntToken) token).intValue(); } } Attribute heightAttribute = ((NamedObj) attribute).getAttribute("_textHeightHint"); if (heightAttribute instanceof Variable) { Token token = ((Variable) heightAttribute).getToken(); if (token instanceof IntToken) { heightValue = ((IntToken) token).intValue(); } } component = addTextArea( name, displayName, attribute.getExpression(), preferredBackgroundColor(attribute), preferredForegroundColor(attribute), heightValue, widthValue); attachParameter(attribute, name); foundStyle = true; } else if (attribute instanceof Variable) { Type declaredType = ((Variable) attribute).getDeclaredType(); Token current = ((Variable) attribute).getToken(); if (declaredType == BaseType.BOOLEAN) { // NOTE: If the expression is something other than // "true" or "false", then this parameter is set // to an expression that evaluates to to a boolean, // and the default Line style should be used. if (attribute.getExpression().equals("true") || attribute.getExpression().equals("false")) { component = addCheckBox(name, displayName, ((BooleanToken) current).booleanValue()); attachParameter(attribute, name); foundStyle = true; } } } // NOTE: Other attribute classes? if (attribute.getVisibility() == Settable.NOT_EDITABLE) { if (component == null) { String defaultValue = attribute.getExpression(); component = addDisplay(name, displayName, defaultValue); attachParameter(attribute, name); foundStyle = true; } else if (component instanceof JTextComponent) { component.setBackground(_background); ((JTextComponent) component).setEditable(false); } else { component.setEnabled(false); } } } catch (IllegalActionException ex) { // Ignore and create a line entry. } } String defaultValue = attribute.getExpression(); if (defaultValue == null) { defaultValue = ""; } if (!(foundStyle)) { addLine( attribute.getName(), attribute.getDisplayName(), defaultValue, preferredBackgroundColor(attribute), preferredForegroundColor(attribute)); // The style itself does this, so we don't need to do it again. attachParameter(attribute, attribute.getName()); } } finally { _addingStyledEntryFor = null; } }
/** Updates the chat message panel in the Swing event thread */ public void updateChatMessagePanel() { // Ensure that we operate in the Swing event thread if (!SwingUtilities.isEventDispatchThread()) { SwingUtilities.invokeLater( new Runnable() { @Override public void run() { updateChatMessagePanel(); } }); return; } // Set the title header titleHeader.setText( chatData != null ? NameUtils.getName(chatData.getId(), NameFormat.MEDIUM) : " "); titleHeader.setVisible(chatData != null); // Only enable send-function when there is chat data, and hence a target sendBtn.setEnabled(chatData != null); messageText.setEditable(chatData != null); // Remove all components from the messages panel messagesPanel.removeAll(); Insets insets = new Insets(0, 2, 2, 2); Insets insets2 = new Insets(6, 2, 0, 2); if (chatData != null && chatData.getMessageCount() > 0) { // First, add a filler component int y = 0; messagesPanel.add( new JLabel(""), new GridBagConstraints(0, y++, 1, 1, 0.0, 1.0, NORTH, VERTICAL, insets, 0, 0)); // Add the messages long lastMessageTime = 0; for (EPDChatMessage message : chatData.getMessages()) { // Check if we need to add a time label if (message.getSendDate().getTime() - lastMessageTime > PRINT_DATE_INTERVAL) { JLabel dateLabel = new JLabel( String.format( message.isOwnMessage() ? "Sent to %s" : "Received %s", Formatter.formatShortDateTimeNoTz( new Date(message.getSendDate().getTime())))); dateLabel.setFont(dateLabel.getFont().deriveFont(9.0f).deriveFont(Font.PLAIN)); dateLabel.setHorizontalAlignment(SwingConstants.CENTER); dateLabel.setForeground(Color.LIGHT_GRAY); messagesPanel.add( dateLabel, new GridBagConstraints(0, y++, 1, 1, 1.0, 0.0, NORTH, HORIZONTAL, insets2, 0, 0)); } // Add a chat message field JPanel msg = new JPanel(); msg.setBorder(new ChatMessageBorder(message)); JLabel msgLabel = new ChatMessageLabel(message.getMsg(), message.isOwnMessage()); msg.add(msgLabel); messagesPanel.add( msg, new GridBagConstraints(0, y++, 1, 1, 1.0, 0.0, NORTH, HORIZONTAL, insets, 0, 0)); lastMessageTime = message.getSendDate().getTime(); } // Scroll to the bottom validate(); scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum()); messagesPanel.repaint(); } else if (chatData == null && noDataComponent != null) { // The noDataComponent may e.g. be a message messagesPanel.add( noDataComponent, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, NORTH, BOTH, insets, 0, 0)); } }
/** * Constructor * * @param compactLayout if false, there will be message type selectors in the panel */ public ChatServicePanel(boolean compactLayout) { super(new BorderLayout()); // Prepare the title header titleHeader.setBackground(getBackground().darker()); titleHeader.setOpaque(true); titleHeader.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); titleHeader.setHorizontalAlignment(SwingConstants.CENTER); add(titleHeader, BorderLayout.NORTH); // Add messages panel scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); messagesPanel.setBackground(UIManager.getColor("List.background")); messagesPanel.setOpaque(false); messagesPanel.setLayout(new GridBagLayout()); messagesPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); add(scrollPane, BorderLayout.CENTER); JPanel sendPanel = new JPanel(new GridBagLayout()); add(sendPanel, BorderLayout.SOUTH); Insets insets = new Insets(2, 2, 2, 2); // Add text area if (compactLayout) { messageText = new JTextField(); ((JTextField) messageText).addActionListener(this); sendPanel.add( messageText, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, NORTH, BOTH, insets, 0, 0)); } else { messageText = new JTextArea(); JScrollPane scrollPane2 = new JScrollPane(messageText); scrollPane2.setPreferredSize(new Dimension(100, 50)); scrollPane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); sendPanel.add( scrollPane2, new GridBagConstraints(0, 0, 1, 2, 1.0, 1.0, NORTH, BOTH, insets, 0, 0)); } // Add buttons ButtonGroup group = new ButtonGroup(); messageTypeBtn = createMessageTypeButton("Send messages", ICON_MESSAGE, true, group); warningTypeBtn = createMessageTypeButton("Send warnings", ICON_WARNING, false, group); alertTypeBtn = createMessageTypeButton("Send alerts", ICON_ALERT, false, group); if (!compactLayout) { JToolBar msgTypePanel = new JToolBar(); msgTypePanel.setBorderPainted(false); msgTypePanel.setOpaque(true); msgTypePanel.setFloatable(false); sendPanel.add( msgTypePanel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, NORTH, NONE, insets, 0, 0)); msgTypePanel.add(messageTypeBtn); msgTypePanel.add(warningTypeBtn); msgTypePanel.add(alertTypeBtn); } if (compactLayout) { sendBtn = new JButton("Send"); sendPanel.add( sendBtn, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, NORTH, NONE, insets, 0, 0)); } else { sendBtn = new JButton("Send", ICON_MESSAGE); sendBtn.setPreferredSize(new Dimension(100, sendBtn.getPreferredSize().height)); sendPanel.add( sendBtn, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, NORTH, NONE, insets, 0, 0)); } sendBtn.setEnabled(false); messageText.setEditable(false); sendBtn.addActionListener(this); }
public void testAssertTextIsEditable() throws Exception { jTextComponent.setEditable(true); assertTrue(textBox.isEditable()); jTextComponent.setEditable(false); assertFalse(textBox.isEditable()); }