001: /*
002: * EncodingPanel.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.Dimension;
015: import java.awt.GridBagConstraints;
016: import java.awt.GridBagLayout;
017:
018: import javax.swing.JComboBox;
019: import javax.swing.JLabel;
020: import javax.swing.JPanel;
021: import workbench.interfaces.EncodingSelector;
022:
023: import workbench.resource.ResourceMgr;
024: import workbench.util.EncodingUtil;
025:
026: /**
027: *
028: * @author support@sql-workbench.net
029: */
030: public class EncodingPanel extends JPanel implements EncodingSelector {
031: protected JComboBox encodings = new JComboBox();
032: private JLabel label;
033:
034: public EncodingPanel() {
035: this (System.getProperty("file.encoding"), true);
036: }
037:
038: public EncodingPanel(String encoding) {
039: this (encoding, true);
040: }
041:
042: public EncodingPanel(String encoding, boolean showLabel) {
043: String[] charsets = EncodingUtil.getEncodings();
044: int count = charsets.length;
045: for (int i = 0; i < count; i++) {
046: encodings.addItem(charsets[i]);
047: }
048:
049: if (encoding != null) {
050: encodings.setSelectedItem(encoding);
051: }
052: Dimension d = new Dimension(300, 22);
053: encodings.setMaximumSize(d);
054: this .setLayout(new GridBagLayout());
055:
056: GridBagConstraints c = new GridBagConstraints();
057: if (showLabel) {
058: label = new JLabel(ResourceMgr.getString("LblFileEncoding"));
059: c.gridx = 0;
060: c.gridy = 0;
061: c.insets = new java.awt.Insets(0, 0, 0, 0);
062: c.fill = java.awt.GridBagConstraints.HORIZONTAL;
063: c.anchor = java.awt.GridBagConstraints.NORTHWEST;
064:
065: this .add(label, c);
066: }
067: c = new GridBagConstraints();
068: c.gridx = 0;
069: c.gridy = 1;
070: c.insets = new java.awt.Insets(5, 0, 0, 0);
071: c.fill = java.awt.GridBagConstraints.HORIZONTAL;
072: c.anchor = java.awt.GridBagConstraints.NORTHWEST;
073: c.weightx = 1.0;
074: c.weighty = 1.0;
075:
076: this .add(encodings, c);
077: }
078:
079: public void setLabelVisible(boolean flag) {
080: if (this .label == null)
081: return;
082: this .label.setVisible(flag);
083: }
084:
085: public boolean isLabelVisible() {
086: if (this .label == null)
087: return false;
088: return this .label.isVisible();
089: }
090:
091: public void setEncoding(String enc) {
092: encodings.setSelectedItem(enc);
093: }
094:
095: public String getEncoding() {
096: String enc = (String) this.encodings.getSelectedItem();
097: return enc;
098: }
099:
100: }
|