Czy to dobry sposób?

0

Czy to dobry sposób na sprawdzenie czy użytkownik podał format pliku i ewentualne dodanie go?

	public static File checkFileName(File file) {
		Pattern pat = Pattern.compile(".*/.png");
		Matcher mat = pat.matcher(file.getName());
		
		if(mat.matches()) {
			return file;
		}
		else{
			File f = new File(file.getAbsolutePath()+".png");
			return f;
		}
	}

Tu cały kod:

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.Window.Type;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Main implements MouseMotionListener, MouseListener{
		JFrame f; JLabel l;
	public static void main(String[] args) {
		new Main();
	}
	public Main() {
		f = new JFrame();
		f.setBounds(0,0,5,5);
		f.setLayout(null);
		f.setAlwaysOnTop(true);
		f.addMouseListener(this);
		f.addMouseMotionListener(this);
		f.setUndecorated(true);
		f.setBackground(new Color(1.0f,1.0f,1.0f,0.1f));
		f.setType(Type.UTILITY);
		l = new JLabel();
		l.setLayout(null);
		l.addMouseListener(this);
		l.setLayout(null);
		l.setBounds(0,0,30,30);
		f.add(l);
		f.setVisible(true);
	}
	public static void setLanguage() {
		UIManager.put("FileChooser.lookInLabelText","Szukaj w");
		UIManager.put("FileChooser.lookInLabelMnemonic",""+KeyEvent.VK_W);
		UIManager.put("FileChooser.saveInLabelText","Zapisz w");
		UIManager.put("FileChooser.saveInLabelMnemonic",""+KeyEvent.VK_W);
		UIManager.put("FileChooser.fileNameLabelText","Nazwa pliku:");
		UIManager.put("FileChooser.fileNameLabelMnemonic",""+KeyEvent.VK_N);
		UIManager.put("FileChooser.folderNameLabelText","Nazwa katalogu:");
		UIManager.put("FileChooser.folderNameLabelMnemonic",""+KeyEvent.VK_N);      
		UIManager.put("FileChooser.filesOfTypeLabelText","Pliki typu:");
		UIManager.put("FileChooser.filesOfTypeLabelMnemonic",""+KeyEvent.VK_P);
		UIManager.put("FileChooser.upFolderToolTipText","Poziom wyżej");
		UIManager.put("FileChooser.homeFolderToolTipText","Pulpit");
		UIManager.put("FileChooser.newFolderToolTipText","Nowy katalog");
		UIManager.put("FileChooser.listViewButtonToolTipText","Lista");
		UIManager.put("FileChooser.detailsViewButtonToolTipText","Szczegóły");
		UIManager.put("FileChooser.fileNameHeaderText","Nazwa");UIManager.put("FileChooser.fileSizeHeaderText","Rozmiar");
		UIManager.put("FileChooser.fileTypeHeaderText","Typ");
		UIManager.put("FileChooser.fileDateHeaderText","Modyfikacja");
		UIManager.put("FileChooser.fileAttrHeaderText","Atrybuty");
		UIManager.put("FileChooser.newFolderErrorText","Blad podczas tworzenia katalogu");
		UIManager.put("FileChooser.saveButtonText","Zapisz");
		UIManager.put("FileChooser.saveButtonMnemonic",""+KeyEvent.VK_Z);
		UIManager.put("FileChooser.openButtonText","Otwórz");
		UIManager.put("FileChooser.openButtonMnemonic",""+KeyEvent.VK_O);
		UIManager.put("FileChooser.cancelButtonText","Anuluj");
		UIManager.put("FileChooser.openButtonMnemonic",""+KeyEvent.VK_R);
		UIManager.put("FileChooser.openDialogTitleText","Otwórz");
		UIManager.put("FileChooser.saveDialogTitleText","Zapisywanie");
		UIManager.put("FileChooser.saveButtonToolTipText","Zapisz");
		UIManager.put("FileChooser.openButtonToolTipText","Otwórz");
		UIManager.put("FileChooser.cancelButtonToolTipText","Anuluj");
		UIManager.put("FileChooser.acceptAllFileFilterText","Wszystkie pliki");
		}	
public static File saveFileDialog() {
		File xyz = null;
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}catch(Exception e) {
			e.printStackTrace();
		}
		setLanguage();
		JFrame frame = new JFrame();
		JFileChooser jf = new JFileChooser();
		jf.setDialogTitle("Zapisz zrzut ekranu");
		jf.setAcceptAllFileFilterUsed(false);
		jf.setFileSelectionMode(JFileChooser.APPROVE_OPTION);
		jf.setFileFilter(new FileNameExtensionFilter("Obraz","png"));
		if (jf.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION){
			xyz = jf.getSelectedFile();
		}
		
		return checkFileName(xyz);
	}		
	public static File checkFileName(File file) {
		Pattern pat = Pattern.compile(".*/.png");
		Matcher mat = pat.matcher(file.getName());
		
		if(mat.matches()) {
			return file;
		}
		else{
			File f = new File(file.getAbsolutePath()+".png");
			return f;
		}
	}

	public static void saveScreenShot() throws AWTException, IOException{
		   Robot robot = new Robot();
		   BufferedImage image = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
		   ImageIO.write(image, "png", saveFileDialog());
		}
	@Override
	public void mouseClicked(MouseEvent e) {
		try {
			saveScreenShot();
		} catch (AWTException | IOException | NullPointerException a) {
			JOptionPane.showMessageDialog(null, "Nie udało się zrobić screenshot'a!", "Błąd!", JOptionPane.ERROR_MESSAGE);
		}
		catch(IllegalArgumentException o) {}
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		f.setBounds(0,0,30,30);
	}
	@Override
	public void mouseExited(MouseEvent e) {
		f.setBounds(0,0,5,5);
	}
	public void mousePressed(MouseEvent arg0) {}
	public void mouseReleased(MouseEvent arg0) {}
	public void mouseDragged(MouseEvent arg0) {}
	public void mouseMoved(MouseEvent arg0) {}
}

0

regexy :D jeszcze może gramatykę napisz i parsuj jakimś CYKiem. Nie mozesz sprawdzić po prostu czy file.getName().endswith(".png")? ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1