Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Logic Familiar Hierarchy - Main.java

Main.java

Caricato da: Nophiq
Scarica il programma completo

  1. /************************************************************************
  2.  * Copyright (C) 19aa Claudio Reggiani alias Nophiq                     *
  3.  * Questo programma è software libero; è lecito ridistribuirlo e/o      *
  4.  * modificarlo secondo i termini della Licenza Pubblica Generica GNU    *
  5.  * come pubblicata dalla Free Software Foundation; o la versione 2      *
  6.  * della licenza o (a scelta) una versione successiva.                  *
  7.  *                                                                      *
  8.  * Questo programma è distribuito  nella speranza che sia utile, ma     *
  9.  * SENZA ALCUNA GARANZIA; senza neppure la garanzia implicita di        *
  10.  * COMMERCIABILITÀ o di APPLICABILITÀ PER UN PARTICOLARE SCOPO. Si      *
  11.  * veda la Licenza Pubblica Generica GNU per avere maggiori dettagli.   *
  12.  *                                                                      *
  13.  * Ognuno dovrebbe avere ricevuto una copia  della Licenza Pubblica     *
  14.  * Generica GNU insieme a questo programma; in caso contrario, la si    *
  15.  * può ottenere dalla Free Software Foundation, Inc., 675 Mass Ave,     *
  16.  * Cambridge, MA 02139, Stati Uniti.                                    *
  17.  *                                                                      *
  18.  * Per contattarmi attraverso posta elettronica: nophiq@virgilio.it     *
  19.  ************************************************************************/
  20.  
  21. package albero;
  22.  
  23. import javax.swing.*;
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import java.io.*;
  27.  
  28. /**
  29.  *
  30.  * @author Nophiq
  31.  */
  32. public class Main extends JFrame implements ActionListener {
  33.   private JMenuBar menuBar;
  34.   private JMenu file, add, view, query, help;
  35.   private JMenuItem hierarchyElaborating, addMember, addRelation, load, save, exit, newFile;
  36.   private JMenuItem newRelation, chiediAlProgramma, about, guide;
  37.   private ObjectPanel panelTree;
  38.   private JPanel panelTreeMother;
  39.   private JScrollPane scrollPanelTree;
  40.   private GridBagConstraints c;
  41.  
  42.   private Graph gr;
  43.  
  44.   public Main() {
  45.     super("Elabora gerarchia famigliare");
  46.     gr = new Graph();
  47.     initialize();
  48.   }
  49.  
  50.   private void initialize() {
  51.     this.setLayout(new GridBagLayout());
  52.     c = new GridBagConstraints();
  53.    
  54.     panelTree = new ObjectPanel(gr);
  55.    
  56.     JScrollBar sH = new JScrollBar(JScrollBar.HORIZONTAL);
  57.     JScrollBar sV = new JScrollBar(JScrollBar.VERTICAL);
  58.     sH.addAdjustmentListener(panelTree);
  59.     sV.addAdjustmentListener(panelTree);
  60.  
  61.     scrollPanelTree = new JScrollPane();
  62.     scrollPanelTree.setViewportView(panelTree);
  63.     scrollPanelTree.setHorizontalScrollBar(sH);
  64.     scrollPanelTree.setVerticalScrollBar(sV);
  65.     scrollPanelTree.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  66.     scrollPanelTree.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  67.    
  68.     panelTreeMother = new JPanel();
  69.     panelTreeMother.setLayout(new BorderLayout());
  70.     panelTreeMother.add(scrollPanelTree, BorderLayout.CENTER);
  71.     panelTreeMother.setMinimumSize(new Dimension(800, 500));
  72.     panelTreeMother.setPreferredSize(new Dimension(800, 500));
  73.     setObjectSetting(c, 10, 10, 1, 1);
  74.     this.add(panelTreeMother, c);
  75.    
  76.     hierarchyElaborating = new JMenuItem("Elabora Gerarchia");
  77.     addMember = new JMenuItem("Aggiungi membro");
  78.     addRelation = new JMenuItem("Aggiungi relazione");
  79.     newRelation = new JMenuItem("Crea relazione");
  80.     newFile = new JMenuItem("Nuovo");
  81.     load = new JMenuItem("Carica");
  82.     save = new JMenuItem("Salva");
  83.     exit = new JMenuItem("Esci");
  84.     about = new JMenuItem("About");
  85.     guide = new JMenuItem("Tutorial");
  86.     chiediAlProgramma = new JMenuItem("Esegui query");
  87.     hierarchyElaborating.addActionListener(this);
  88.     addMember.addActionListener(this);
  89.     addRelation.addActionListener(this);
  90.     newFile.addActionListener(this);
  91.     load.addActionListener(this);
  92.     save.addActionListener(this);
  93.     exit.addActionListener(this);
  94.     newRelation.addActionListener(this);
  95.     chiediAlProgramma.addActionListener(this);
  96.     about.addActionListener(this);
  97.     guide.addActionListener(this);
  98.    
  99.     file = new JMenu("File");
  100.     file.add(newFile);
  101.     file.add(load);
  102.     file.add(save);
  103.     file.addSeparator();
  104.     file.add(exit);
  105.    
  106.     add = new JMenu("Aggiungi");
  107.     add.add(addMember);
  108.     add.add(addRelation);
  109.     add.add(newRelation);
  110.    
  111.     view = new JMenu("Visualizza");
  112.     view.add(hierarchyElaborating);
  113.    
  114.     query = new JMenu("Query");
  115.     query.add(chiediAlProgramma);
  116.    
  117.     help = new JMenu("Help");
  118.     help.add(about);
  119.     help.addSeparator();
  120.     help.add(guide);
  121.    
  122.     menuBar = new JMenuBar();
  123.     menuBar.add(file);
  124.     menuBar.add(add);
  125.     menuBar.add(view);
  126.     menuBar.add(query);
  127.     menuBar.add(help);
  128.     this.setJMenuBar(menuBar);
  129.    
  130.     this.pack();
  131.     this.setVisible(true);
  132.   }
  133.  
  134.   private void setObjectSetting(GridBagConstraints c, int gridx, int gridy, int gridheight, int gridwidth) {
  135.     c.gridx = gridx;  // Posizionamento Orizzontale
  136.     c.gridy = gridy;  // Posizionamento Verticale
  137.     c.gridheight = gridheight;
  138.     c.gridwidth = gridwidth;
  139.   }
  140.  
  141.   // Gestisco la pressione di bottoni
  142.   public void actionPerformed(ActionEvent ev) {
  143.     Object src = ev.getSource();
  144.     if (src == hierarchyElaborating) panelTree.getHierarchy();
  145.     if (src == addMember) new AddNewMember(panelTree);
  146.     if (src == addRelation) new AddNewRelation(panelTree);
  147.     if (src == newFile) panelTree.newFile();
  148.     if (src == newRelation) new CreateRelation(panelTree);
  149.     if (src == chiediAlProgramma) new Query(panelTree);
  150.     if (src == about) new About();
  151.     if (src == guide) new Guide();
  152.    
  153.     if (src == load) {
  154.       JFileChooser fileChooser = new JFileChooser();
  155.       int response = fileChooser.showOpenDialog(this);
  156.       if (response==JFileChooser.APPROVE_OPTION) {
  157.         String messaggio = "";
  158.         File fileTxt = fileChooser.getSelectedFile();
  159.  
  160.         javax.swing.filechooser.FileFilter txtFilter = new javax.swing.filechooser.FileFilter() {
  161.           public boolean accept(File f) {
  162.             if (!f.isDirectory() && f.getName().toLowerCase().endsWith(".pl")) return true;
  163.             else return false;
  164.           }
  165.           public String getDescription() {return "File .pl";}
  166.         };
  167.  
  168.           // Controllo l'estensione
  169.         if (txtFilter.accept(fileTxt)) {
  170.           // Carico la teoria
  171.           gr.clearAll();
  172.           panelTree.setTheory(fileTxt);
  173.         }
  174.       }
  175.     }
  176.    
  177.     if (src == save) {
  178.       JFileChooser salva = new JFileChooser();
  179.       salva.setDialogTitle("Salva Posizione - Salvare in formato .pl");
  180.       int scelta = salva.showSaveDialog(this);
  181.       if (scelta == JFileChooser.APPROVE_OPTION) {
  182.         File fFile = salva.getSelectedFile();
  183.         try {
  184.           FileOutputStream fos = new FileOutputStream(fFile);
  185.           PrintStream ps = new PrintStream(fos);
  186.           ps.println(panelTree.getTheory());
  187.         }
  188.         catch (IOException ex) {
  189.           ex.printStackTrace();
  190.         }
  191.       }
  192.     }
  193.    
  194.     if (src == exit) {
  195.       this.dispose();
  196.       System.exit(1);
  197.     }
  198.    
  199.   }
  200.  
  201.   public static void main(String[] args) {
  202.     Main m = new Main();
  203.   }
  204. }