• Tidak ada hasil yang ditemukan

5.3 Implementasi Kelas

5.3.1 Implementasi Kelas HalamanUtama

Pada kelas HalamanUtama terdapat beberapa method yang berkaitan dengan fungsi utama antara lain pilihFile, pilihXLS, pilihCSV, tampilTabelBasisdata, cekAtribut, cekAtribut2, hapusAtribut, distribusiAtribut, submitData, prosesData dan simpanHasil.

public void setSisa(int sisa) { this.sisa = sisa;

}

public boolean isFirstArray() { return firstArray;

}

public void setFirstArray(boolean firstArray) { this.firstArray = firstArray;

}

public List<Integer> getIsi_blok() { return isi_blok;

}

public void setIsi_blok(List<Integer> isi_blok) { this.isi_blok = isi_blok;}}

CheckBoxTableModel cb_table = new CheckBoxTableModel(); SeleksiAtribut fm;

DatabaseConnection con;

public HalamanUtama(DatabaseConnection db, String namaTabel) { initComponents();

tampilTabelBasisdata(db, namaTabel); }

public void pilihFile() {

JFileChooser chooser = new JFileChooser(); int returnVal = chooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { String nama_file = chooser.getSelectedFile().getPath(); String[] potong = nama_file.split("\\.");

String pola = "[0-9.]*"; String pola2 = "[.]*";

if (potong[1].equals("xls")) { //FILE EXCEL pilihXLS(chooser, nama_file, pola, pola2); } else if (potong[1].equals("csv")) { //FILE CSV pilihCSV(chooser, nama_file, pola, pola2); } else {

JOptionPane.showMessageDialog(null, "File yang dipilih harus berformat .xls atau .csv"); }

} }

public void pilihXLS(JFileChooser chooser, String nama_file, String pola, String pola2) { cb_table.removeRow2();

File excelFile = chooser.getSelectedFile(); pathField.setText(nama_file);

Vector columnNames = new Vector(); Vector data = new Vector();

try {

Workbook workbook = Workbook.getWorkbook(excelFile); Sheet sheet = workbook.getSheets()[0];

for (int row = 0; row < sheet.getRows(); row++) { Vector rows = new Vector();

for (int column = 0; column < sheet.getColumns(); column++) { if (row == 0) { columnNames.addElement(sheet.getCell(column, row).getContents()); } else { rows.addElement(sheet.getCell(column, row).getContents()); } } if (row != 0) { data.addElement(rows); } }

hasilOutlierTextArea.setText(null);

DefaultTableModel model = new DefaultTableModel(data, columnNames); preprocessTabel.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); preprocessTabel.setModel(model);

updateCellWidth(preprocessTabel); jumlahDataField.setText("" + data.size()); distAtributButton.setEnabled(true);

for (int i = 0; i < model.getColumnCount(); i++) { for (int j = 0; j < model.getRowCount(); j++) { if (i == 0) {

if (model.getValueAt(j, 0).toString().isEmpty()) {

JOptionPane.showMessageDialog(null, "Label data Anda tidak lengkap. Silahkan lengkapi label data "

+ "pada file asli Anda."); i = model.getColumnCount() - 1; break; } } else { if (model.getValueAt(j, i).toString().isEmpty() || !model.getValueAt(j, i).toString().matches(pola) || model.getValueAt(j, i).toString().matches(pola2)) {

HalamanMissingValues m = new HalamanMissingValues(model); m.setVisible(true); i = model.getColumnCount() - 1; break; } } } }

// AbstractTableModel dengan JCheckBox for (int i = 0; i < model.getColumnCount(); i++) { fm = new SeleksiAtribut(); fm.setAtribut2(model.getColumnName(i)); fm.setPilih(false); cb_table.add(i, fm); } deteksiOutlierTabel.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); deteksiOutlierTabel.setModel(model); updateCellWidth(deteksiOutlierTabel); jumDataField.setText(jumlahDataField.getText());

hapusAtributTable.setModel(cb_table); hapusAtributButton.setEnabled(true); checkAllButton.setEnabled(true); uncheckAllButton.setEnabled(true); submitDataButton.setEnabled(true); } catch (Exception e) { } }

public void pilihCSV(JFileChooser chooser, String nama_file, String pola, String pola2) {

File csvFile = chooser.getSelectedFile(); pathField.setText(nama_file);

Vector columnNames = new Vector(); Vector data = new Vector();

String line; try {

BufferedReader br = new BufferedReader(new FileReader(csvFile)); StringTokenizer st = new StringTokenizer(br.readLine(), ","); while (st.hasMoreTokens()) {

columnNames.addElement(st.nextToken()); }

while ((line = br.readLine()) != null) {

StringTokenizer st2 = new StringTokenizer(line, ","); Vector row = new Vector();

while (st2.hasMoreTokens()) { row.addElement(st2.nextToken()); } data.addElement(row); } br.close();

DefaultTableModel model = new DefaultTableModel(data, columnNames); preprocessTabel.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); preprocessTabel.setModel(model); updateCellWidth(preprocessTabel); pathField.setText(csvFile.getPath()); jumlahDataField.setText("" + data.size()); distAtributButton.setEnabled(true);

for (int i = 0; i < model.getColumnCount(); i++) { for (int j = 0; j < model.getRowCount(); j++) { if (model.getValueAt(j, i).toString().isEmpty()

|| !model.getValueAt(j, i).toString().matches(pola) || model.getValueAt(j, i).toString().matches(pola2)) {

JOptionPane.showMessageDialog(null, "Data Anda belum lengkap. "

+ "Silahkan lengkapi data Anda pada file asli."); i = model.getColumnCount() - 1;

break; }

} }

// AbstractTableModel dengan JCheckBox List<String> isiData = new ArrayList<String>(); for (int i = 0; i < model.getColumnCount(); i++) { fm = new SeleksiAtribut(); fm.setAtribut2(model.getColumnName(i)); fm.setPilih(false); cb_table.add(i, fm); }

deteksiOutlierTabel.setAutoResizeMode(JTable.AUTO_RESIZE_OF F);

deteksiOutlierTabel.setModel(model); updateCellWidth(deteksiOutlierTabel);

jumDataField.setText(jumlahDataField.getText()); hapusAtributTable.setModel(cb_table); hapusAtributButton.setEnabled(true); checkAllButton.setEnabled(true); uncheckAllButton.setEnabled(true); submitDataButton.setEnabled(true); } catch (Exception e1) {

} }

public void tampilTabelBasisdata(DatabaseConnection db, String namaTabel) {

try {

Database con = new Database(); ResultSet rs;

rs = con.displayTable(db.getConnection(), namaTabel); java.sql.ResultSetMetaData meta = rs.getMetaData(); int coloumCount = meta.getColumnCount();

Vector columnName = new Vector(); Vector data = new Vector();

columnName.clear();

for (int i = 1; i <= coloumCount; i++) {

columnName.add(meta.getColumnName(i)); }

data.clear(); while (rs.next()) {

Vector data2 = new Vector();

for (int i = 1; i <= coloumCount; i++) { data2.add(rs.getString(i));

}

data2.add("\n"); data.add(data2); }

DefaultTableModel model = new DefaultTableModel(data, columnName); preprocessTabel.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); preprocessTabel.setModel(model); updateCellWidth(preprocessTabel); jumlahDataField.setText("" + data.size()); pathField.setText("TABEL : " + namaTabel); cb_table.removeRow2(); String pola = "[0-9.]*"; String pola2 = "[.]*";

for (int i = 0; i < model.getColumnCount(); i++) { for (int j = 0; j < model.getRowCount(); j++) {

if (i == 0) {

if (model.getValueAt(j, 0) == null) {

JOptionPane.showMessageDialog(null, "Label data Anda tidak lengkap. Silahkan lengkapi label data pada file asli Anda.");

i = model.getColumnCount() - 1; break; } } else { if (model.getValueAt(j, i) == null || !model.getValueAt(j, i).toString().matches(pola) || model.getValueAt(j, i).toString().matches(pola2)) {

JOptionPane.showMessageDialog(null, "Data Anda belum lengkap. "

+ "Silahkan lengkapi data Anda pada file asli."); i = model.getColumnCount() - 1;

}

}

} }

List<String> isiData = new ArrayList<String>();

for (int i = 0; i < model.getColumnCount(); i++) { fm = new SeleksiAtribut(); fm.setAtribut2(model.getColumnName(i)); fm.setPilih(false); cb_table.add(i, fm); } deteksiOutlierTabel.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); deteksiOutlierTabel.setModel(model); updateCellWidth(deteksiOutlierTabel); jumDataField.setText(jumlahDataField.getText()); hapusAtributTable.setModel(cb_table); hapusAtributButton.setEnabled(true); checkAllButton.setEnabled(true); uncheckAllButton.setEnabled(true); submitDataButton.setEnabled(true); } catch (Exception e) { } }

public boolean cekAtribut(String x, List<String> hapus) { boolean y = false;

for (int i = 0; i < hapus.size(); i++) { if (hapus.get(i).equals(x)) { y = true; } } return y; }

public boolean cekAtribut2(String x, Vector kolom) { boolean y = false;

for (int i = 0; i < kolom.size(); i++) { if (kolom.get(i).equals(x)) { y = true; } } return y; }

//http://stackoverflow.com/questions/13013989/how-to-adjust-jtable-columns-to-fit-the-longest-content-in-column-cells

private void updateCellWidth(JTable t) { adjustRowSizes(t);

for (int i = 0; i < t.getColumnCount(); i++) { adjustColumnSizes(t, i, 2);

} }

//http://stackoverflow.com/questions/13013989/how-to-adjust-jtable-columns-to-fit-the-longest-content-in-column-cells

private void adjustRowSizes(JTable jTable) {

for (int row = 0; row < jTable.getRowCount(); row++) { int maxHeight = 0;

for (int column = 0; column < jTable.getColumnCount(); column++) { TableCellRenderer cellRenderer = jTable.getCellRenderer(row, column); Object valueAt = jTable.getValueAt(row, column);

Component tableCellRendererComponent =

cellRenderer.getTableCellRendererComponent(jTable, valueAt, false, false, row, column);

int heightPreferable =

tableCellRendererComponent.getPreferredSize().height;

maxHeight = Math.max(heightPreferable, maxHeight); } jTable.setRowHeight(row, maxHeight); } } //http://stackoverflow.com/questions/13013989/how-to-adjust-jtable-columns-to-fit-the-longest-content-in-column-cells

public void adjustColumnSizes(JTable table, int column, int margin) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();

TableColumn col = colModel.getColumn(column); int width;

TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) {

renderer = table.getTableHeader().getDefaultRenderer(); }

Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);

width = comp.getPreferredSize().width; for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, column);

comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, column), false, false, r, column);

int currentWidth = comp.getPreferredSize().width; width = Math.max(width, currentWidth);

}

width += 2 * margin;

col.setPreferredWidth(width); col.setWidth(width);

}

private void tandaiSemua() {

for (int i = 0; i < cb_table.getRowCount(); i++) { cb_table.setValueAt(true, i, 1);

} }

private void batalTandai() {

for (int i = 0; i < cb_table.getRowCount(); i++) { cb_table.setValueAt(false, i, 1);

} }

private void hapusAtribut() {

List<String> hapus_atribut = new ArrayList<String>(); for (int i = 0; i < cb_table.getRowCount(); i++) { if (cb_table.getValueAt(i, 1).equals(true)) {

hapus_atribut.add(cb_table.getValueAt(i, 0).toString()); }

}

TableModel model = preprocessTabel.getModel(); int column = model.getColumnCount();

int row = model.getRowCount(); Vector columnNames = new Vector(); Vector data = new Vector();

for (int i = 0; i < cb_table.getRowCount(); i++) {

if (!cekAtribut(cb_table.getValueAt(i, 0).toString(), hapus_atribut)) { columnNames.addElement(cb_table.getValueAt(i, 0));

} }

for (int i = 0; i < row; i++) { Vector rows = new Vector(); for (int j = 0; j < column; j++) {

if (cekAtribut2(model.getColumnName(j).toString(), columnNames)) { rows.addElement(model.getValueAt(i, j)); } } data.addElement(rows); }

DefaultTableModel new_model = new DefaultTableModel(data, columnNames);

preprocessTabel.setModel(new_model);

if (!data.isEmpty() && !columnNames.isEmpty()) { updateCellWidth(preprocessTabel); } if (columnNames.isEmpty()) { jumlahDataField.setText(null); submitDataButton.setEnabled(false); checkAllButton.setEnabled(false); uncheckAllButton.setEnabled(false); hapusAtributButton.setEnabled(false); distAtributButton.setEnabled(false); pathField.setText(null); jTabbedPane1.setSelectedComponent(panelPreprocess); jTabbedPane1.setEnabledAt(1, false); } cb_table.removeRow(); }

private void distribusiAtribut() {

List<String> daftarAtribut = new ArrayList<String>(); int jumlahAtribut = preprocessTabel.getColumnCount() - 1; for (int i = 1; i <= jumlahAtribut; i++) {

daftarAtribut.add(preprocessTabel.getColumnName(i)); }

HalamanDistribusiAtribut distribusiDialog = new

HalamanDistribusiAtribut(this, daftarAtribut, preprocessTabel); distribusiDialog.setVisible(true);

}

private void submitData() {

TableModel model = preprocessTabel.getModel();

deteksiOutlierTabel.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); deteksiOutlierTabel.setModel(model); updateCellWidth(deteksiOutlierTabel); jumDataField.setText(jumlahDataField.getText()); jTabbedPane1.setSelectedComponent(panelDeteksiOutlier); jTabbedPane1.setEnabledAt(1, true); } //deteksi outlier

private void prosesData() { String pola = "[0-9.]*"; String pola2 = "[.]*";

if (jumDataField.getText().equals("")) {

JOptionPane.showMessageDialog(null, "Anda belum memasukkan data"); atributDField.setText(null);

atributMField.setText(null);

jTabbedPane1.setSelectedComponent(panelPreprocess); prosesButton.setEnabled(false);

} else if (atributDField.getText().length() > 0 && atributMField.getText().length() > 0

&& ((!atributDField.getText().matches(pola) || atributDField.getText().matches(pola2))

|| (!atributMField.getText().matches(pola) || atributMField.getText().matches(pola2)))) {

JOptionPane.showMessageDialog(null, "Nilai M dan D harus berupa bilangan bulat atau desimal!");

atributDField.setText(null); atributMField.setText(null); prosesButton.setEnabled(false);

} else if (atributDField.getText().equals("") || atributMField.getText().equals("")) {

JOptionPane.showMessageDialog(null, "Nilai M dan D harus diisi!"); prosesButton.setEnabled(false);

} else {

double m = Integer.parseInt(atributMField.getText()); double d = Double.parseDouble(atributDField.getText()); GraphController g = new GraphController();

g.deteksiOutlier(deteksiOutlierTabel.getRowCount());

hasilOutlierTextArea.setText(g.tampilHasil(deteksiOutlierTabel, m, d, jumDataField.getText(), pathField.getText()));

prosesButton.setEnabled(false); atributDField.setText(null);

atributMField.setText(null); simpanButton.setEnabled(true); }

}

private void simpanHasil() {

JFileChooser fileChooser = new JFileChooser(new File("D:/"));

fileChooser.setFileFilter(new FileNameExtensionFilter("Microsoft Word (*.doc)", "doc"));

fileChooser.setFileFilter(new FileNameExtensionFilter("Text Documents (*.txt)", "txt"));

FileOutputStream file_output_stream;

int returnValue = fileChooser.showSaveDialog(this); if (returnValue == JFileChooser.APPROVE_OPTION) { String filename = fileChooser.getSelectedFile().getPath(); String extension = fileChooser.getFileFilter().getDescription(); File ff = new File(filename);

//buat file output stream

if (extension.equals("Microsoft Word (*.doc)")) { ff = new File(filename + ".doc");

} else if (extension.equals("Text Documents (*.txt)")) { ff = new File(filename + ".txt");

} try {

file_output_stream = new FileOutputStream(ff); } catch (FileNotFoundException fe) {

JOptionPane.showMessageDialog(this, "Penyimpanan Hasil Deteksi Outlier Gagal! "

+ "Periksa kembali lokasi Anda akan menyimpan file"); //fe.printStackTrace();

return; }

try {

//ambil teks dari JTextArea

String hasil_text_area = hasilOutlierTextArea.getText(); String[] baca = hasil_text_area.split("\\n");

FileWriter outFile = new FileWriter(ff);

PrintWriter out = new PrintWriter(outFile, true); for (int i = 0; i < baca.length; i++) {

out.println("" + baca[i].toString()); }

file_output_stream.close(); } catch (IOException ie) {

JOptionPane.showMessageDialog(this, "Penyimpanan Hasil Deteksi Outlier Gagal !");

atributMField.setText(null); simpanButton.setEnabled(true); } } private void simpanHasil() {

JFileChooser fileChooser = new JFileChooser(new File("D:/"));

fileChooser.setFileFilter(new FileNameExtensionFilter("Microsoft Word (*.doc)", "doc"));

fileChooser.setFileFilter(new FileNameExtensionFilter("Text Documents (*.txt)", "txt"));

FileOutputStream file_output_stream;

int returnValue = fileChooser.showSaveDialog(this); if (returnValue == JFileChooser.APPROVE_OPTION) { String filename = fileChooser.getSelectedFile().getPath(); String extension = fileChooser.getFileFilter().getDescription(); File ff = new File(filename);

//buat file output stream

if (extension.equals("Microsoft Word (*.doc)")) { ff = new File(filename + ".doc");

} else if (extension.equals("Text Documents (*.txt)")) { ff = new File(filename + ".txt");

} try {

file_output_stream = new FileOutputStream(ff); } catch (FileNotFoundException fe) {

JOptionPane.showMessageDialog(this, "Penyimpanan Hasil Deteksi Outlier Gagal! "

+ "Periksa kembali lokasi Anda akan menyimpan file"); //fe.printStackTrace();

return; } try {

//ambil teks dari JTextArea

String hasil_text_area = hasilOutlierTextArea.getText(); String[] baca = hasil_text_area.split("\\n");

FileWriter outFile = new FileWriter(ff);

PrintWriter out = new PrintWriter(outFile, true); for (int i = 0; i < baca.length; i++) {

out.println("" + baca[i].toString()); }

file_output_stream.close(); } catch (IOException ie) {

JOptionPane.showMessageDialog(this, "Penyimpanan Hasil Deteksi Outlier Gagal !");

}

JOptionPane.showMessageDialog(this, "Hasil Deteksi Outlier Telah Berhasil Disimpan di " + filename); }

simpanButton.setEnabled(false); }

Dokumen terkait