/* * Copyright 2015 Igor Maznitsa. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.igormaznitsa.sciareto.ui.editors.mmeditors; import com.igormaznitsa.mindmap.model.logger.Logger; import com.igormaznitsa.mindmap.model.logger.LoggerFactory; import com.igormaznitsa.sciareto.ui.UiUtils; import java.awt.Toolkit; import java.net.URI; import java.net.URISyntaxException; import javax.swing.ImageIcon; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import com.igormaznitsa.sciareto.ui.Focuser; public final class UriEditPanel extends javax.swing.JPanel { private static final long serialVersionUID = -6683682013891751388L; private static final Logger LOGGER = LoggerFactory.getLogger(UriEditPanel.class); private static final ImageIcon IMAGE_OK = new ImageIcon(UiUtils.loadIcon("tick16.png")); //NOI18N private static final ImageIcon IMAGE_BAD = new ImageIcon(UiUtils.loadIcon("cancel.png")); //NOI18N private static final ImageIcon IMAGE_QUESTION = new ImageIcon(UiUtils.loadIcon("question16.png")); //NOI18N public UriEditPanel(final String uri) { initComponents(); this.textFieldURI.setText(uri == null ? "" : uri); //NOI18N this.textFieldURI.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { validateUri(); } @Override public void removeUpdate(DocumentEvent e) { validateUri(); } @Override public void changedUpdate(DocumentEvent e) { validateUri(); } }); new Focuser(this.textFieldURI); validateUri(); } public String getText() { return this.textFieldURI.getText().trim(); } private void validateUri() { final String text = this.textFieldURI.getText().trim(); this.labelValidator.setText(""); //NOI18N if (text.isEmpty()) { this.labelValidator.setIcon(IMAGE_QUESTION); } else { try { final URI uri = URI.create(text); if (uri.getScheme()!=null && uri.getHost()!=null) { this.labelValidator.setIcon(IMAGE_OK); } else { throw new NullPointerException(); } } catch (Exception ex) { this.labelValidator.setIcon(IMAGE_BAD); } } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; labelBrowseCurrentLink = new javax.swing.JLabel(); textFieldURI = new javax.swing.JTextField(); labelValidator = new javax.swing.JLabel(); butonReset = new javax.swing.JButton(); setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10)); setLayout(new java.awt.GridBagLayout()); labelBrowseCurrentLink.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/url_link.png"))); // NOI18N java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("com/igormaznitsa/nbmindmap/i18n/Bundle"); // NOI18N labelBrowseCurrentLink.setToolTipText(bundle.getString("UriEditPanel.labelBrowseCurrentLink.toolTipText_1")); // NOI18N labelBrowseCurrentLink.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); labelBrowseCurrentLink.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); labelBrowseCurrentLink.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { labelBrowseCurrentLinkMouseClicked(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.ipadx = 10; add(labelBrowseCurrentLink, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1000.0; add(textFieldURI, gridBagConstraints); labelValidator.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); labelValidator.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/question16.png"))); // NOI18N gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.ipadx = 10; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; add(labelValidator, gridBagConstraints); butonReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cross16.png"))); // NOI18N butonReset.setFocusable(false); butonReset.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { butonResetActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 0; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; add(butonReset, gridBagConstraints); }// </editor-fold>//GEN-END:initComponents private void labelBrowseCurrentLinkMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelBrowseCurrentLinkMouseClicked if (evt.getClickCount() > 1) { try { UiUtils.browseURI(new URI(this.getText().trim()), false); } catch (URISyntaxException ex) { LOGGER.error("Can't start browser for URI syntax error", ex); //NOI18N Toolkit.getDefaultToolkit().beep(); } } }//GEN-LAST:event_labelBrowseCurrentLinkMouseClicked private void butonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butonResetActionPerformed this.textFieldURI.setText(""); //NOI18N }//GEN-LAST:event_butonResetActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton butonReset; private javax.swing.JLabel labelBrowseCurrentLink; private javax.swing.JLabel labelValidator; private javax.swing.JTextField textFieldURI; // End of variables declaration//GEN-END:variables }