if(!selectedFile.canWrite()){
if(selectedFile.exists() && !selectedFile.canWrite()){
    private void setupComponents(final String defaultUser, final String uri) {        GridBagLayout grid = new GridBagLayout();        setLayout(grid);        GridBagConstraints c = new GridBagConstraints();        final int inset = 5;        c.insets = new Insets(inset, inset, inset, inset);                JLabel label = new JLabel("Username");        c.gridx = 0;        c.gridy = 0;        c.gridwidth = 1;        c.anchor = GridBagConstraints.EAST;        c.fill = GridBagConstraints.NONE;        grid.setConstraints(label, c);        add(label);                username = new JTextField(defaultUser, 12);        c.gridx = 1;        c.gridy = 0;        c.gridwidth = 2;        c.anchor = GridBagConstraints.WEST;        c.fill = GridBagConstraints.HORIZONTAL;        grid.setConstraints(username, c);        add(username);                label = new JLabel("Password");        c.gridx = 0;        c.gridy = 1;        c.gridwidth = 1;        c.anchor = GridBagConstraints.EAST;        c.fill = GridBagConstraints.NONE;        grid.setConstraints(label, c);        add(label);                password = new JPasswordField(12);        c.gridx = 1;        c.gridy = 1;        c.gridwidth = 2;        c.anchor = GridBagConstraints.WEST;        c.fill = GridBagConstraints.HORIZONTAL;        grid.setConstraints(password, c);        add(password);                label = new JLabel("Type");        c.gridx = 0;        c.gridy = 2;        c.gridwidth = 1;        c.anchor = GridBagConstraints.EAST;        c.fill = GridBagConstraints.NONE;        grid.setConstraints(label, c);        add(label);                type = new JComboBox();        type.addItem("Remote");                type.addItem("Local");        type.setSelectedIndex(uri.equals(URI_LOCAL) ? TYPE_LOCAL : TYPE_REMOTE);        type.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                switch (type.getSelectedIndex()) {                    case TYPE_LOCAL:                        cur_url.setText(URI_LOCAL);                        cur_url.setEnabled(false);                        break;                    case TYPE_REMOTE:                        cur_url.setText(!uri.equals(URI_LOCAL) ? uri : URI_REMOTE);                        cur_url.setEnabled(true);                        break;                }            }        });        c.gridx = 1;        c.gridy = 2;        c.gridwidth = 2;        c.anchor = GridBagConstraints.WEST;        grid.setConstraints(type, c);        add(type);                label = new JLabel("URL");        c.gridx = 0;        c.gridy = 3;        c.gridwidth = 1;        c.anchor = GridBagConstraints.EAST;        c.fill = GridBagConstraints.NONE;        grid.setConstraints(label, c);        add(label);                cur_url = new JTextField(uri, 20);        cur_url.setEnabled(!uri.equals(URI_LOCAL));        c.gridx = 1;        c.gridy = 3;        c.gridwidth = 2;        c.anchor = GridBagConstraints.WEST;        c.fill = GridBagConstraints.HORIZONTAL;        grid.setConstraints(cur_url, c);        add(cur_url);                label = new JLabel("Title");        c.gridx = 0;        c.gridy = 4;        c.gridwidth = 1;        c.insets = new Insets(20, inset, inset, inset);        c.anchor = GridBagConstraints.EAST;        c.fill = GridBagConstraints.NONE;        grid.setConstraints(label, c);        add(label);                title = new JTextField();        title.getDocument().addDocumentListener(new DocumentListener() {            public void insertUpdate(DocumentEvent arg0) {                btnAddFavourite.setEnabled(title.getText().length() > 0);            }            public void removeUpdate(DocumentEvent arg0) {                btnAddFavourite.setEnabled(title.getText().length() > 0);            }            public void changedUpdate(DocumentEvent arg0) {                btnAddFavourite.setEnabled(title.getText().length() > 0);            }        });        c.gridx = 1;        c.gridy = 4;        c.gridwidth = 2;        c.insets = new Insets(20, inset, inset, inset);        c.anchor = GridBagConstraints.WEST;        c.fill = GridBagConstraints.HORIZONTAL;        grid.setConstraints(title, c);        add(title);                label = new JLabel("Favourites");        c.gridx = 0;        c.gridy = 5;        c.gridwidth = 1;        c.gridheight = 4;        c.insets = new Insets(inset, inset, inset, inset);        c.anchor = GridBagConstraints.NORTHEAST;        c.fill = GridBagConstraints.NONE;        grid.setConstraints(label, c);        add(label);                favouritesModel = new DefaultListModel();        Favourite[] f = loadFavourites();        for (int i=0; i < f.length; favouritesModel.addElement(f[i++]));        favourites = new JList(favouritesModel);        favourites.addListSelectionListener(new ListSelectionListener() {            public void valueChanged(ListSelectionEvent e) {                boolean selection = favourites.getSelectedIndex() >= 0;                btnLoadFavourite.setEnabled(selection);                btnRemoveFavourite.setEnabled(selection);            }        });        favourites.addMouseListener(new MouseAdapter() {            public void mouseClicked(MouseEvent e) {                super.mouseClicked(e);                if (e.getClickCount() == 2 && favourites.getSelectedIndex() >= 0) {                    Favourite f = (Favourite)favourites.getSelectedValue();                    title.setText(f.getName());                    username.setText(f.getUsername());                    password.setText(f.getPassword());                    type.setSelectedIndex(URI_LOCAL.equals(f.getUrl()) ? TYPE_LOCAL : TYPE_REMOTE);                    cur_url.setText(f.getUrl());                }            }        });        JScrollPane scroll = new JScrollPane(favourites);        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);        scroll.setPreferredSize(new Dimension(200, 130));        c.gridx = 1;        c.gridy = 5;        c.gridheight = 4;        c.insets = new Insets(inset, inset, inset, inset);        c.anchor = GridBagConstraints.NORTHWEST;        c.fill = GridBagConstraints.HORIZONTAL;        grid.setConstraints(scroll, c);        add(scroll);                btnLoadFavourite = new JButton("Select");        btnLoadFavourite.setToolTipText("Select favourite");        btnLoadFavourite.setEnabled(false);        btnLoadFavourite.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                Favourite f = (Favourite)favourites.getSelectedValue();                title.setText(f.getName());                username.setText(f.getUsername());                password.setText(f.getPassword());                type.setSelectedIndex(URI_LOCAL.equals(f.getUrl()) ? TYPE_LOCAL : TYPE_REMOTE);                cur_url.setText(f.getUrl());            }        });        c.gridx = 2;        c.gridy = 5;        c.gridwidth = 1;        c.gridheight = 1;        c.anchor = GridBagConstraints.WEST;        c.insets = new Insets(inset, inset, 15, inset);        c.fill = GridBagConstraints.HORIZONTAL;        grid.setConstraints(btnLoadFavourite, c);        add(btnLoadFavourite);                btnAddFavourite = new JButton("Save");        btnAddFavourite.setToolTipText("Save settings");        btnAddFavourite.setEnabled(false);        btnAddFavourite.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                String t = title.getText();                for (int i=0; i < favouritesModel.getSize(); i++) {                    if (favouritesModel.elementAt(i).equals(t)) {                        int result = JOptionPane.showConfirmDialog(LoginPanel.this, "A connection with this name already exists. Ok to overwrite?", "Conflict", JOptionPane.YES_NO_OPTION);                        if (result == JOptionPane.NO_OPTION) {                            return;                        }                        favouritesModel.remove(i);                        break;                    }                }                Favourite f = new Favourite(                        title.getText(),                        username.getText(),                        new String(password.getPassword()),                        cur_url.getText()                        );                favouritesModel.addElement(f);                storeFavourites(favouritesModel);            }        });        c.gridx = 2;        c.gridy = 6;        c.gridwidth = 1;        c.gridheight = 1;        c.anchor = GridBagConstraints.WEST;        c.insets = new Insets(inset, inset, inset, inset);        c.fill = GridBagConstraints.HORIZONTAL;        grid.setConstraints(btnAddFavourite, c);        add(btnAddFavourite);                btnRemoveFavourite = new JButton("Remove");        btnRemoveFavourite.setEnabled(false);        btnRemoveFavourite.setToolTipText("Remove favourite");        btnRemoveFavourite.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                favouritesModel.remove(favourites.getSelectedIndex());                btnRemoveFavourite.setEnabled(false);                btnLoadFavourite.setEnabled(false);                storeFavourites(favourites.getModel());                repaint();            }        });        c.gridx = 2;        c.gridy = 7;        c.gridwidth = 1;        c.gridheight = 1;        c.anchor = GridBagConstraints.WEST;        c.insets = new Insets(inset, inset, 15, inset);        c.fill = GridBagConstraints.HORIZONTAL;        grid.setConstraints(btnRemoveFavourite, c);        add(btnRemoveFavourite);                btnExportFavourite = new JButton("Export");        btnExportFavourite.setEnabled(true);        btnExportFavourite.setToolTipText("Export favourites to file");        btnExportFavourite.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                JFileChooser chooser = new JFileChooser();                chooser.showSaveDialog(LoginPanel.this);                File selectedFile = chooser.getSelectedFile();                                if(selectedFile==null){                    JOptionPane.showMessageDialog(LoginPanel.this, "No favourites file selected", "Error", JOptionPane.ERROR_MESSAGE);                    return;                }                                if(!selectedFile.canWrite()){                    JOptionPane.showMessageDialog(LoginPanel.this, "Cannot write selected file", "Error", JOptionPane.ERROR_MESSAGE);                    return;                }                exportFavourites(selectedFile);                repaint();            }        });        c.gridx = 2;        c.gridy = 8;        c.gridwidth = 1;        c.gridheight = 1;        c.anchor = GridBagConstraints.WEST;        c.fill = GridBagConstraints.HORIZONTAL;        c.insets = new Insets(inset, inset, inset, inset);        grid.setConstraints(btnExportFavourite, c);        add(btnExportFavourite);                btnImportFavourite = new JButton("Import");        btnImportFavourite.setEnabled(true);        btnImportFavourite.setToolTipText("Import favourites from file");        btnImportFavourite.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                JFileChooser chooser = new JFileChooser();                chooser.showOpenDialog(LoginPanel.this);                File selectedFile = chooser.getSelectedFile();                                if(selectedFile==null){                    JOptionPane.showMessageDialog(LoginPanel.this, "No favourites file selected", "Error", JOptionPane.ERROR_MESSAGE);                    return;                }                                if(!selectedFile.canRead()){                    JOptionPane.showMessageDialog(LoginPanel.this, "Cannot read selected file", "Error", JOptionPane.ERROR_MESSAGE);                    return;                }                                importFavourites(selectedFile);                                repaint();            }        });        c.gridx = 2;        c.gridy = 9;        c.gridwidth = 1;        c.gridheight = 1;        c.anchor = GridBagConstraints.WEST;        c.fill = GridBagConstraints.HORIZONTAL;        grid.setConstraints(btnImportFavourite, c);        add(btnImportFavourite);                JPanel spacer = new JPanel();        c.gridx = 2;        c.gridy = 8;        c.gridwidth = 1;        c.gridheight = 1;        c.fill = GridBagConstraints.BOTH;        grid.setConstraints(spacer, c);        add(spacer);            }