gdt.jgui.entity.folder.JFileOpenItem Maven / Gradle / Ivy
package gdt.jgui.entity.folder;
/*
* Copyright 2016 Alexander Imas
* This file is part of JEntigrator.
JEntigrator is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JEntigrator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JEntigrator. If not, see .
*/
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Properties;
import java.util.logging.Logger;
import javax.activation.MimetypesFileTypeMap;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import org.apache.commons.codec.binary.Base64;
import gdt.data.entity.ArchiveFileFilter;
import gdt.data.entity.ArchiveHandler;
import gdt.data.entity.BaseHandler;
import gdt.data.entity.EntityHandler;
import gdt.data.entity.facet.BookmarksHandler;
import gdt.data.entity.facet.FolderHandler;
import gdt.data.grain.Core;
import gdt.data.grain.Locator;
import gdt.data.grain.Sack;
import gdt.data.store.Entigrator;
import gdt.data.store.FileExpert;
import gdt.jgui.base.JBaseNavigator;
import gdt.jgui.console.JConsoleHandler;
import gdt.jgui.console.JFacetOpenItem;
import gdt.jgui.console.JItemPanel;
import gdt.jgui.console.JRequester;
import gdt.jgui.console.WContext;
import gdt.jgui.console.WUtils;
import gdt.jgui.tool.JTextEditor;
/**
* This class represents a file item within the
* folder panel context.
* @author imasa
*
*/
public class JFileOpenItem extends JItemPanel implements WContext{
private static final long serialVersionUID = 1L;
JMenuItem editItem;
JMenuItem importItem;
boolean debug=false;
/**
* The constructor.
*/
public static final String[] IMAGE_EXTENSIONS = new String[]{
"gif", "png", "bmp","jpg","jpeg"
};
public JFileOpenItem(){
super();
popup = new JPopupMenu();
popup.addPopupMenuListener(new PopupMenuListener(){
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
popup.removeAll();
//System.out.println("JFileOpenItem:locator="+locator$);
//System.out.println("JFileOpenItem:file name="+Locator.getProperty(locator$, JFolderPanel.FILE_NAME));
popup.removeAll();
JMenuItem openItem=new JMenuItem("Open");
popup.add(openItem);
openItem.setHorizontalTextPosition(JMenuItem.RIGHT);
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(timer==null)
createTimer();
timer.start();
}
});
JMenuItem showPath=new JMenuItem("Show path");
popup.add(showPath);
showPath.setHorizontalTextPosition(JMenuItem.RIGHT);
showPath.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
Properties locator=Locator.toProperties(locator$);
String entihome$=locator.getProperty(Entigrator.ENTIHOME);
String filePath$=entihome$+"/"+locator.getProperty(JFolderPanel.FILE_PATH);
JTextEditor te=new JTextEditor();
String teLocator$=te.getLocator();
teLocator$=Locator.append(teLocator$, Entigrator.ENTIHOME, entihome$);
teLocator$=Locator.append(teLocator$, JTextEditor.TEXT,filePath$);
locator$=Locator.remove(locator$, BaseHandler.HANDLER_METHOD);
String requesterResponceLocator$=Locator.compressText(locator$);
teLocator$=Locator.append(teLocator$,JRequester.REQUESTER_RESPONSE_LOCATOR,requesterResponceLocator$);
JConsoleHandler.execute(console,teLocator$);
}catch(Exception ee){
Logger.getLogger(JFileOpenItem.class.getName()).info(ee.toString());
}
}
});
JMenuItem folderItem=new JMenuItem("Open folder");
popup.add(folderItem);
folderItem.setHorizontalTextPosition(JMenuItem.RIGHT);
folderItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
Properties locator=Locator.toProperties(locator$);
String entihome$=locator.getProperty(Entigrator.ENTIHOME);
String filePath$=entihome$+"/"+locator.getProperty(JFolderPanel.FILE_PATH);
File itemFile=new File(filePath$);
File folder=itemFile.getParentFile();
Desktop.getDesktop().open(folder);
}catch(Exception ee){
Logger.getLogger(JFileOpenItem.class.getName()).info(ee.toString());
}
}
});
String fname$=Locator.getProperty(locator$, JFolderPanel.FILE_NAME);
if(isArchiveFile(fname$)){
String entihome$=Locator.getProperty(locator$,Entigrator.ENTIHOME);
String filePath$=entihome$+"/"+Locator.getProperty(locator$,JFolderPanel.FILE_PATH);
if(debug)
System.out.println("JFileOpenItem:file path="+filePath$);
String contentType$=ArchiveHandler.detectContentOfArchive(filePath$);
if(debug)
System.out.println("JFileOpenItem:content type="+contentType$);
if(ArchiveHandler.ARCHIVE_CONTENT_ENTITIES.equals(contentType$)){
popup.addSeparator();
JMenuItem importItem=new JMenuItem("Import");
popup.add(importItem);
importItem.setHorizontalTextPosition(JMenuItem.RIGHT);
importItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
Properties locator=Locator.toProperties(locator$);
String filePath$=locator.getProperty(JFolderPanel.FILE_PATH);
if(debug)
System.out.println("FileOpenItem:import file="+filePath$);
}catch(Exception ee){
Logger.getLogger(JFileOpenItem.class.getName()).info(ee.toString());
}
}
});
}
if(ArchiveHandler.ARCHIVE_CONTENT_DATABASE.equals(contentType$)){
popup.addSeparator();
JMenuItem extractItem=new JMenuItem("Extract");
popup.add(extractItem);
extractItem.setHorizontalTextPosition(JMenuItem.RIGHT);
extractItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
Properties locator=Locator.toProperties(locator$);
String filePath$=locator.getProperty(JFolderPanel.FILE_PATH);
if(debug)
System.out.println("FileOpenItem:extract file="+filePath$);
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File(System.getProperty("user.home")));
chooser.setDialogTitle("Extract database");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(JFileOpenItem.this) == JFileChooser.APPROVE_OPTION) {
String parentDir$=chooser.getSelectedFile().getPath();
String base$=FileExpert.getBasicFilename(filePath$);
File entihome=new File(parentDir$+"/"+base$);
if(entihome.exists()){
if(entihome.isFile())
entihome.delete();
else{
try{FileExpert.clear(entihome.getPath());}catch(Exception ee){}
entihome.delete();
}
}
entihome.mkdir();
//System.out.println("BaseNavigator.install extension="+extension$);
ArchiveHandler.extractEntities(entihome.getPath(), filePath$);
}
}catch(Exception ee){
Logger.getLogger(JFileOpenItem.class.getName()).info(ee.toString());
}
}
});
}
}
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
// TODO Auto-generated method stub
}
});
}
private static boolean isArchiveFile(String file$){
try{
ArchiveFileFilter aff=new ArchiveFileFilter();
return aff.accept(null, file$);
}catch(Exception e){
Logger.getLogger(JFileOpenItem.class.getName()).info(e.toString());
return false;
}
}
@Override
public String getWebView(Entigrator entigrator, String locator$) {
try{
if(debug)
System.out.println("JFileOpenItem:getWebView:locator="+locator$);
Properties locator=Locator.toProperties(locator$);
String webHome$=locator.getProperty(WContext.WEB_HOME);
String entityKey$=locator.getProperty(EntityHandler.ENTITY_KEY);
String entityLabel$=locator.getProperty(EntityHandler.ENTITY_LABEL);
String facetHandlerClass$=locator.getProperty(JFacetOpenItem.FACET_HANDLER_CLASS);
if(entityLabel$==null&&entityKey$!=null)
entityLabel$=entigrator.indx_getLabel(entityKey$);
String webRequester$=locator.getProperty(WContext.WEB_REQUESTER);
String title$=locator.getProperty(Locator.LOCATOR_TITLE);
StringBuffer sb=new StringBuffer();
sb.append("");
sb.append("");
sb.append("");
sb.append(WUtils.getMenuBarScript());
sb.append(WUtils.getMenuBarStyle());
sb.append("");
sb.append("");
sb.append(" ");
sb.append("Base: ");
sb.append(entigrator.getBaseName());
sb.append(" Entity: ");
sb.append(entityLabel$);
sb.append(" ");
sb.append("Facet: ");
if(BookmarksHandler.class.getName().equals(facetHandlerClass$))
sb.append("Bookmarks");
if(FolderHandler.class.getName().equals(facetHandlerClass$))
sb.append("Folder");
sb.append(" ");
sb.append("Context: ");
sb.append("Slideshow");
sb.append(" ");
sb.append("
");
String[]sa=null;
Properties imLocator=new Properties();
MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap();
mimetypesFileTypeMap.addMimeTypes("image png jpg jpeg bmp gif ");
ArrayListsl=new ArrayList();
Hashtable ht=new Hashtable();
String fname$;
String fpath$;
String ftitle$;
if(BookmarksHandler.class.getName().equals(facetHandlerClass$)){
Sack entity=entigrator.getEntityAtKey(entityKey$);
Core[] ca=entity.elementGet("jbookmark");
for(Core c:ca){
fname$=Locator.getProperty(c.value, JFolderPanel.FILE_NAME);
if(fname$!=null)
if("image".equalsIgnoreCase(mimetypesFileTypeMap.getContentType(fname$))){
imLocator=Locator.toProperties(c.value);
ftitle$=c.type;
fpath$=entigrator.getEntihome()+"/"+imLocator.getProperty(JFolderPanel.FILE_PATH);
if(fname$!=null&&fpath$!=null)
sl.add(ftitle$);
ht.put(ftitle$, fpath$);
}
}
}
if(FolderHandler.class.getName().equals(facetHandlerClass$)){
File folder=new File(entigrator.getEntihome()+"/"+entityKey$);
File[] fa=folder.listFiles();
for(File f:fa){
fname$=f.getName();
if("image".equalsIgnoreCase(mimetypesFileTypeMap.getContentType(fname$))){
ftitle$=f.getName();
fpath$=f.getPath();
sl.add(ftitle$);
ht.put(ftitle$, fpath$);
}
}
}
if(sl.size()>0){
Collections.sort(sl);
sb.append("");
sb.append(" ");
}
sa=getNavigators(sl,title$);
if(sa!=null){
if(sa[0]!=null){
sb.append(" ");
}
if(sa[1]!=null){
sb.append(" ");
}
if(debug){
System.out.println("JFileOpenItem:getWebView:prev locator="+sa[0]);
System.out.println("JFileOpenItem:getWebView:next locator="+sa[1]);
}
}
sb.append("");
sb.append("");
ftitle$=sl.get(0);
if(title$!=null)
ftitle$=title$;
fpath$=ht.get(ftitle$);
BufferedImage bm=ImageIO.read(new File(fpath$));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write( bm, "png", bos );
bos.flush();
byte[] ba = bos.toByteArray();
bos.close();
String icon$= Base64.encodeBase64String(ba);
sb.append("");
sb.append("");
sb.append("");
sb.append("");
return sb.toString();
}catch(Exception e){
Logger.getLogger(JFileOpenItem.class.getName()).severe(e.toString());
}
return null;
}
@Override
public String getWebConsole(Entigrator entigrator, String locator$) {
// TODO Auto-generated method stub
return null;
}
private String[] getNavigators(ArrayListsl,String title$){
try{
if(title$==null)
title$=sl.get(0);
int i=0;
for(String s:sl){
if(title$.equals(s)){
String[] sa=new String[2];
if(i>0)
sa[0]=sl.get(i-1);
else
sa[0]=null;
if(sl.size()>(i+1))
sa[1]=sl.get(i+1);
else
sa[1]=null;
return sa;
}
i++;
}
}catch(Exception e){
Logger.getLogger(JFileOpenItem.class.getName()).severe(e.toString());
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy